Creating Android Notepad App: 3. Create and Save Text file on External Storage

In this part we will learn how to create new notepad file and save it to on External SD. Also how we can manage all the add edit delete functionalities from Note Manager class and create application directory on external SD to store all the text files created by our app.
So let’s start with Note Manager first.


Topics Covered

I. Creating Note Manager Class to manage add/edit/delete


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

Right click on project and Create new java class file and name it NoteManager.java and save it under default package of the project.

Now define constructors for the class. On with Context argument and on without argument. Also define a String variable name “path” and set its value as External Storage with directory name “Notes”.
See the code below.

ii. Create separate application directory on External Storage to save text files


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

In the Note Manager Class define a new public function named CreateNewDirectoy(). In this function, first we will check the “Note” directory is exist or not. Only we will create directory if it is not exist on External SD. Or we will throw the error log message as a “dir already exist”. See the code below.

Now it’s time to call this function, when application starts. And when application starts, the Main activity runs first as we have setup it in AndroidManifest.xml. So let’s open Main.java and define new variable called “nm” as NoteManager and call the method CreateNewDirectory() from class.

iii. Creating dialog to get file name input from user


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

Once you create you note with some text, you need to save it. But how will you identify your file text content inside the file in the list of files. Exactly… by file name. So let’s write the code to get file name input.
To get filename input from user we need a dialog where we can get keyboard input from user. Open Note.java and define SaveNote() function and call it into Save Action item by selecting on Action Bar. See the code below.

Creating Notepad Application in Android


Inside the >SaveNote() function, write the code to create dialog box and get filename input.

Creating Notepad Application in Android

iv. Save files in directory on External Storage with Input name


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

Once we done with dialog input, we need to pass the file name to save function. But before that we have to write the code for Save the created file. Open the NoteManager.java file and create a new function SaveNote() with the two parameters. i.e. File Name, Text Content).


Now call this function into Note Activity with two required parameters.
See the below.

Download Source

0 replies

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 *