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.

9 thoughts on “An outrageously simple note taking android app made better (part 2)

  1. Hello,

    I believe you forgot to mention that you have to update the onCreate method. Remove the code that was done in the previous part because it is redundant, since you introduced the database. The onCreate method should look like:

    protected void onCreate(Bundle savedInstanceState)
    EditText text = (EditText) findViewById(;

    • Sorry but I don’t have that version of code, but only the final code. So neither can accept or reject your comment. Later on I probably will be able to look into it.

      Thanks anyway for dropping in and looking into code.

  2. hello, i have been following this tutorial and i am having problems i have created the notes class and code but how are they linked to the main application.
    i am using eclypse ide if that helps.
    thanks in advanced :)

  3. This design is spectacular! You definitely know how to keep a reader entertained.
    Between your wit and your videos, I was almost moved to start my own blog (well,
    almost…HaHa!) Excellent job. I really
    enjoyed what you had to say, and more than that, how you presented it.
    Too cool!

  4. I was wondering if you ever thought of changing the structure of your site?
    Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content so
    people could connect with it better. Youve got an awful lot of text for only having 1 or two images.
    Maybe you could space it out better?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s