Simple Notepad App Android Example

In this tutorial I have shared a very simple notepad app android example. I have used file handling to read and write data. The notepad consists of three options to create new file, save and open a file. When you click on save or open button an alert dialog opens where you have to enter the file name. Internal memory is used to save the text file. At the end I have shared a link to download the apk for this example so that you can directly install and run it in your phone.

 

Also Read: How to Make a Calculator App for Android

 

Simple Notepad App Android Example

Simple Notepad App Android Example 1

Simple Notepad App Android Example 2

 

MainActivity.java

It contains the actual source code responsible for all the working.

package com.thecrazyprogrammer.www.notepadapp;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.FileInputStream;
import java.io.FileOutputStream;


public class MainActivity extends Activity {

    Button newButton,saveButton,openButton;
    EditText text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        newButton=(Button)findViewById(R.id.newButton);
        saveButton=(Button)findViewById(R.id.saveButton);
        openButton=(Button)findViewById(R.id.openButton);
        text=(EditText)findViewById(R.id.text);
    }

    public void buttonAction(View v) {
        final EditText fileName=new EditText(this);
        AlertDialog.Builder ad=new AlertDialog.Builder(this);
        ad.setView(fileName);

        if (v.getId() == R.id.saveButton) {
            ad.setMessage("Save File");

            ad.setPositiveButton("Save",new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    try {
                        FileOutputStream fout=openFileOutput(fileName.getText().toString()+".txt",MODE_WORLD_READABLE);
                        fout.write(text.getText().toString().getBytes());
                    } catch (Exception e) {
                        Toast.makeText(getApplicationContext(), "Error Occured: "+e,Toast.LENGTH_LONG).show();
                     }
                }
            });

            ad.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

            ad.show();

        }

        if(v.getId()==R.id.openButton) {
            ad.setMessage("Open File");

            ad.setPositiveButton("Open",new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    int c;
                    text.setText("");

                    try {
                        FileInputStream fin = openFileInput(fileName.getText().toString()+".txt");

                        while ((c = fin.read()) != -1)
                        {
                            text.setText((text.getText().toString() + Character.toString((char) c)));
                        }
                    }catch (Exception e) {
                        Toast.makeText(getApplicationContext(), "Error Occured: "+e,Toast.LENGTH_LONG).show();
                    }
                }
            });

            ad.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

            ad.show();
        }

        if(v.getId()==R.id.newButton) {
            text.setText("");
        }
    }
}

 

activity_main.xml

This xml file contains the code for the design of notepad app.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.com.thecrazyprogrammer.www.notepadapp"
    android:orientation="vertical"
    android:weightSum="10">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9"
        android:orientation="vertical">

         <EditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:inputType="textMultiLine"
            android:gravity="top"
            android:id="@+id/text"
            android:scrollbars = "vertical"/>
        </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="New"
            android:id="@+id/newButton"
            android:layout_weight="1"
            android:onClick="buttonAction"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Save"
            android:id="@+id/saveButton"
            android:onClick="buttonAction"
            android:layout_weight="1"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Open"
            android:id="@+id/openButton"
            android:onClick="buttonAction"
            android:layout_weight="1"/>
    </LinearLayout>

</LinearLayout>

 

– Download Notepad App Apk –

 

If you have any doubts regarding above notepad android example then comment below.

6 thoughts on “Simple Notepad App Android Example”

  1. I want to open up the file I saved with utf-8.
    I use swedish language with letters å, ä and ö.
    Do anyone know some way to solve that.
    Very good example and explaining!
    LOG

Leave a Comment

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