An outrageously simple note taking android app made better (part 2)

Hi all, in this post we will make the note taking app made in previous post better .

We will make the app to store multiple notes and shall display them in our main screen.

At the very first we will change the app icon. For this put an icon file inres/drawable-* folders ( one for each size)  and change the string in AndroidManifest.xml as below.


Here the notes256 is file name of icon I put in drawable-* folders.

Notice the new icon above.

Now to store multiple notes  we will need database. We will follow the pattern championed by masters and create the notes class to store well, notes 🙂 and NotesDatabase to create, store and retrieve notes.

Here is the notes class

public class Notes {

    int _id;
    String _note;

    public Notes(){
    public Notes(int id, String note){
        this._id = id;
        this._note = note;
    public Notes(String note){
        this._note = note;
    public int getID(){
        return this._id;
    public void setID(int id){
        this._id = id;
    public String getNote(){
        return this._note;
    public void setNote(String note){
        this._note = note;

Code is self explanatory it just store notes and id for database operations and onscreen UI.

Now here is the

We will understand the code by step by step

    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";

These fields define the database name, table name and table key name.

                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NOTE + " TEXT" + ")";

Above statements in onCreate method prepares the create query and when executed by db.execSQL creates the table.

        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_NOTE, notes.getNote());

        db.insert(TABLE_NOTES, null, values);

Above ones in addNote takes a Notes object and store its values in database.

        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_NOTES, new String[] { KEY_ID,
                KEY_NOTE }, KEY_ID + "=?",
                new String[] { String.valueOf(id) }, null, null, null, null);
        if (cursor != null)

        Notes notes = new Notes(Integer.parseInt(cursor.getString(0)),
        return notes;
Similarly the above ones reads the database and create a notes object and returns it.
        List<Notes> notesList = new ArrayList<Notes>();
        String selectQuery = "SELECT  * FROM " + TABLE_NOTES;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()) {
            do {
                Notes notes = new Notes();

            } while (cursor.moveToNext());

        return notesList;

Above listed ones get all notes from database.

There are more methods but we use only the above listed code.

In change the onClickSave method to be like this

    public void onClickSave(View theButton) {
        EditText text = (EditText) findViewById(;
        NotesDatabase db =new NotesDatabase(this);

        db.addNote(new Notes(text.getText().toString()));

This will save the note to database.

Now change the onCreate method of MyActivity to be like this.

    public void onCreate(Bundle savedInstanceState) {
        NotesDatabase db =new NotesDatabase(this);

        List<Notes> notes = db.getAllNotes();

        for ( Notes nt: notes) {
            final LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            TextView b = (TextView) inflater.inflate(R.layout.textviews,null);
            LinearLayout lLayout = (LinearLayout)findViewById(;

This requires some explanation as this dynamically adds the TextView defined in textviews.xml file and adds them to main activity.

final LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER

Above code creates a inflater object whose inflate method gets the text view.

TextView b = (TextView) inflater.inflate(R.layout.textviews,null);

This gets the TextView from below textview.xml

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android=""


android:text="Press to close"


Below line gets the layout object for layout of main activity whose id has been set to layout1 in main.xml.

addView adds the TextView to the layout dynamically.

LinearLayout lLayout = (LinearLayout)findViewById(;

Now build and run the app

Even at this stage there is lot to be desired. We will keep improving in next post.

Thanks to Sai Geetha for inflator trick, and Ravi Tamada, Lars Vogel for database pattern.