Create Notes App for Android Devices
Q: Develop a program that allows users to create and store notes on an Android device.
- Android
- Senior level question
Explore all the latest Android interview questions and answers
ExploreMost Recent & up-to date
100% Actual interview focused
Create Android interview for FREE!
To create an Android app that allows users to create and store notes, you can follow these steps:
1. Create a new Android project in Android Studio and add a new layout file for the note taking activity.
2. In the layout file, add a text field for the user to enter the note and a button to save the note.
3. In the Java code for the activity, add an OnClickListener to the save button to save the note to a SQLite database.
4. Create a database helper class that extends SQLiteOpenHelper and provides methods for creating and upgrading the database, as well as adding and retrieving notes.
5. Add a RecyclerView to the layout file to display a list of saved notes.
6. In the Java code for the activity, create an adapter for the RecyclerView and use it to display the saved notes.
7. Add a long press listener to the RecyclerView items to allow the user to delete a note.
Here's some sample code for the MainActivity:
public class MainActivity extends AppCompatActivity {And here's some sample code for the NotesDbHelper class:
private EditText noteEditText;
private RecyclerView notesRecyclerView;
private NotesAdapter notesAdapter;
private NotesDbHelper dbHelper; protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); noteEditText = findViewById(R.id.noteEditText); notesRecyclerView = findViewById(R.id.notesRecyclerView); notesRecyclerView.setLayoutManager(
setContentView(R.layout.activity_main); noteEditText = findViewById(R.id.noteEditText);
notesRecyclerView = findViewById(R.id.notesRecyclerView);
notesRecyclerView.setLayoutManager(new LinearLayoutManager(this)); dbHelper = dbHelper = new NotesDbHelper(this); notesAdapter =
notesAdapter = new NotesAdapter(dbHelper.getAllNotes()); notesRecyclerView.setAdapter(notesAdapter); findViewById(R.id.saveButton).setOnClickListener(
notesRecyclerView.setAdapter(notesAdapter); findViewById(R.id.saveButton).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String note = noteEditText.getText().toString();
if (!TextUtils.isEmpty(note)) {
long id = dbHelper.addNote(note);
Note newNote = new Note(id, note); notesAdapter.addNote(newNote); noteEditText.setText(
notesAdapter.addNote(newNote);
noteEditText.setText(""); } } }); notesRecyclerView.addOnItemTouchListener(
}
}
}); notesRecyclerView.addOnItemTouchListener(new RecyclerTouchListener(this, notesRecyclerView, new ClickListener() {
public void onClick(View view, int position) {
// handle click
} public void onLongClick(View view, int position) { dbHelper.deleteNote(notesAdapter.getNoteId(position)); notesAdapter.removeNoteAt(position); } })); }
dbHelper.deleteNote(notesAdapter.getNoteId(position));
notesAdapter.removeNoteAt(position);
}
}));
} protected void onDestroy() { dbHelper.close();
dbHelper.close();
super.onDestroy(); } }
}
}
public class NotesDbHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "NotesDatabase"; private static final String TABLE_NOTES = "Notes";
private static final String KEY_ID = "id";
private static final String KEY_NOTE = "note"; private static final String CREATE_TABLE_NOTES = "CREATE TABLE " + TABLE_NOTES + "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ KEY_NOTE + " TEXT)"; public NotesDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION); }
} public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_TABLE_NOTES); }
db.execSQL(CREATE_TABLE_NOTES);
} public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL(
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NOTES); onCreate(db); }
onCreate(db);
} public long addNote(String note) {
SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_NOTE, note);
values.put(KEY_NOTE, note); long id = db.insert(TABLE_NOTES, null, values); db.close();
db.close();
return id; } }


