An outrageously simple note taking android app made even more better (part 4)


In this post we will add code to display context menu to “edit and replace”, and “delete” note.

Adding a context menu is easy ( well, most of the things are easy once you know how 🙂 ). Just call  registerForContextMenu(b); in for loop in onResume of MyActivity.java.

Further override onCreateContextMenu to add items in context menu.

    public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        currentNote = ((TextView)v).getText().toString();
        // Create your context menu here
        menu.setHeaderTitle("Context Menu");
        menu.add(0, v.getId(), 0, "Edit n Replace");
        menu.add(0, v.getId(), 1, "Delete");
    }

And you shall see a context menu as below

we are also saving the current note here in currentNote.

We also need to override onContextItemSelected method as below.

    public boolean onContextItemSelected(MenuItem item) {
        // Call your function to perform for buttons pressed in a context menu
        // can use item.getTitle() or similar to find out button pressed
        // item.getItemID() will return the v.getID() that we passed before
        super.onContextItemSelected(item);

        if ( item.getTitle().toString().equals("Delete")){
            NotesDatabase db =new NotesDatabase(this);

            db.searchAndDelete(currentNote);
            onResume();
        }
        else if ( item.getTitle().toString().equals("Edit n Replace")) {
            Intent intent = new Intent(this, EditNoteActivity.class);
            intent.putExtra("ACTION","oldnote");
            intent.putExtra("ACTION2","replace");
            intent.putExtra(EXTRA_MESSAGE,currentNote);
            startActivity(intent);
        }

        return true;
    }

If the item selected in menu is “Delete” then a new function searchAndDelete in NotesDatabase.java deletes the note.

If it’s “Edit n Replace” then the EditNoteActivity is started with intent carrying extra messages to describe the action needs to be performed in aptly named ACTION1 and ACTION2.

Below is searchAndDelete function in NotesDatabase.java

    public void searchAndDelete ( String note) {
        String selectQuery = "SELECT  " +  KEY_ID + " FROM " + TABLE_NOTES + " WHERE " + KEY_NOTE + " = " + "'" + note + "';";
        Notes notes= new Notes();

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

        cursor.moveToFirst();
        notes.setID(Integer.parseInt(cursor.getString(0)));

        deleteNote(notes);
    }

Very simply note is searched in database for its id, and when found complete notes objet is passed to deleteNote for actual deletion.

Below is EditNoteActivity.java

public class EditNoteActivity extends Activity {
    public final static String EXTRA_MESSAGE = "com.example.NoteTaking.MESSAGE";
    String action = new String(),action2 = new String(),oldnote = new String();

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.editnote);
        Intent intent = getIntent();
        EditText text = (EditText) findViewById(R.id.editText1);

        Bundle extras = intent.getExtras();
        if ( extras.getString("ACTION").equals("oldnote")) {
            action = "oldnote";
            if ( extras.getString("ACTION2").equals("replace")) {
                action2 = "replace";
            }
            oldnote = extras.getString(EXTRA_MESSAGE);
            text.setText(extras.getString(EXTRA_MESSAGE));
        }
    }

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

        if ( action.equals("oldnote") && action2.equals("replace")) {
            db.searchAndDelete(oldnote);
        }
        db.addNote(new Notes(text.getText().toString()));
        finish();
    }
    public void onClickBack(View theButton) {
        finish();
    }
}

Here action, action2 and oldnote store the messages passed in intent either onClickTextView1 or onContextItemSelected. Based on action messages sent onClickSave will either delete the note and save the new, or just save the new note leaving old one intact.

Again lot of people helped from stackoverflow.com, many thanks to them.

Here is the complete source code, licensed under GPLv2.

http://www.mediafire.com/download.php?prbz0i39iaxc09i

I hope these post give a lot of learning to readers as they had been to mine. Of Course it’s still not perfect and we will keep improving it in future.

Thanks for reading. 🙂

Update

Adding this functionality broke the old code. In EditNoteActivity.java we need to count number of messages passed in intent before trying to access the third message. As we send 2 message for normal editing and three for replace. Here is the code which will be in EditNoteActivity.java.

public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.editnote);
        Intent intent = getIntent();
        EditText text = (EditText) findViewById(R.id.editText1);

        Bundle extras = intent.getExtras();
        Set<String> keypair = extras.keySet();

        if ( extras.getString("ACTION").equals("oldnote")) {
            action = "oldnote";
            if ( keypair.size() == 3 ) {
            if ( extras.getString("ACTION2").equals("replace")) {
                action2 = "replace";
            }
            }
            oldnote = extras.getString(EXTRA_MESSAGE);
            text.setText(extras.getString(EXTRA_MESSAGE));
        }
    }

We are taking the key value pair using extras.keySet and checking it’s size equals to 3 before accessing the third message.

6 thoughts on “An outrageously simple note taking android app made even more better (part 4)

  1. Thanks for making this series of tutorials I have been thinking of trying out some Android development and your practical tutorial is great

  2. Hey! I know this is kind of off topic but I was wondering which blog platform are you using for this site?
    I’m getting tired of WordPress because I’ve had problems with
    hackers and I’m looking at alternatives for another platform. I would be fantastic if you could point me in the direction of a good platform.

Leave a comment