Creating Android Notepad App: 5. Read and Edit Text file from External Storage

In previous 4 parts we learnt how we have created text file and save it on External Storage. But If you want to do some changes in saved files which you created, then you need to open it, edit it whatever you want and save it the same file without enter new name of the file, it must be saved in same file and same location.
So, in this part I am going to cover how we can read text file from External Storage and Edit it.


Topics Covered

i. Opening Note Activity by passing File info with Intent


Warning: Cannot modify header information - headers already sent by (output started at /home/bajayk/public_html/wp-content/plugins/all-in-one-seo-pack/aioseop_class.php:3983) in /home/bajayk/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_wp.class.php on line 220

To read/edit the File we need to open the text file in separate activity/screen for that we will re-use same create new text note activity. Which we have created in Part 3.
So first we need to apply the onItemClickListener to ListView and pass the File name information to Note activity using Intent. So we can refer that file name in Note Activity to open that Text file.
See the code below how we to write the logic for it in Main.java.

ii. Reading Text File


Warning: Cannot modify header information - headers already sent by (output started at /home/bajayk/public_html/wp-content/plugins/all-in-one-seo-pack/aioseop_class.php:3983) in /home/bajayk/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_wp.class.php on line 220

Into the Note Activity to read the text file we need to first get the file name from the Intent data which is sent from Main activity. So open the Note.java and get the intent extras Here we are using same new note activity to edit/delete functionality too. So we have to take care about the new file and edit file logic to.
See the code below how the logic is implemented.


Notice the the above code. Here we are checking the extras coming from Main Activity. And if it is not null then it means it has a filename and we have to read the text in edit mode. If it is not that means it is new Note mode.

Creating Android Notepad App

iii. Edit and Save Text File


Warning: Cannot modify header information - headers already sent by (output started at /home/bajayk/public_html/wp-content/plugins/all-in-one-seo-pack/aioseop_class.php:3983) in /home/bajayk/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_wp.class.php on line 220

Now it’s time to save the edited file again with updated text content. While save the file we will call the same SaveNote() function which we are calling for save new file. You will the input dialog code to request filename input from user. Here we will again use the same logic as we have used while reading file in above topic.
So first we will check is the file name is null? If yes, then it is new note mode and we have to display filename input dialog. And if no, the file name we already have and note activity is in edit mode. So we will just grab the content and save it.
See the code below how the SaveNote() function is modified here.

Save and Run the project.

Creating Android Notepad App

Download Gradle Source

7 replies
    • Ajay Bagujar
      Ajay Bagujar says:

      Good question Daniel,

      I really like to write 6 part of this tutorial “How to delete note”. But in between you can try this.

      –CODE–
      // Count total checked items
      final int checkedCount = lv_filenames.getCheckedItemCount();

      // retrieve selected items and delete them out
      SparseBooleanArray selected = adapter.getSelectedIds();
      for (int i = (selected.size() – 1); i >= 0; i–) {
      if (selected.valueAt(i)) {
      FileName selectedItem = adapter
      .getItem(selected.keyAt(i));
      adapter.remove(selectedItem);
      new NoteManager().deleteFile(selectedItem.getName());
      }
      }

      // Diplay message of total deleted items
      Toast.makeText(Main.this, checkedCount + ” Items Removed”, Toast.LENGTH_SHORT).show();

      mode.finish(); // Action picked, so close the CAB
      –/CODE–

      Reply
    • Ajay Bagujar
      Ajay Bagujar says:

      You may define a function in Note.java and write entire delete logic into the function. Then call it on onClick event while user will select delete command from option menu.

      ————————————–
      @Override
      public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
      switch (item.getItemId()) {
      case R.id.delete:

      // Count total checked items
      final int checkedCount = lv_filenames.getCheckedItemCount();

      // retrieve selected items and delete them out
      SparseBooleanArray selected = adapter.getSelectedIds();
      for (int i = (selected.size() – 1); i >= 0; i–) {
      if (selected.valueAt(i)) {
      FileName selectedItem = adapter
      .getItem(selected.keyAt(i));
      adapter.remove(selectedItem);
      new NoteManager().deleteFile(selectedItem.getName());
      }
      }

      // Diplay message of total deleted items
      Toast.makeText(Main.this, checkedCount + ” Items Removed”, Toast.LENGTH_SHORT).show();

      mode.finish(); // Action picked, so close the CAB
      return true;
      default:
      return false;
      }
      }
      ————————————–

      Reply
  1. Martin
    Martin says:

    Hello

    That is very nice and helpful tutorial 🙂
    But there is one thing that doesn’t work for me…
    When I have multiple notes.. in every note there is same text inside. So for example, when I edit note1.txt, write “apples” in it and hit save, that same text “apples” will appear on note3.txt. too.

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *