Real Time Android Development
- makeiton




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.

 

Simple Notepad App Android Example

Simple Notepad App Android Example 2

 

MainActivity.java

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



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<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="in.co.makeiton.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>

Things to Learn


Android adding Facebook Comments Widget in App Android Creating Gmail Like Inbox using RecyclerView Android Downloading File by Showing Progress Bar
How to make Text to Speech in Android How to Design Android Login and Registration Screen How to switch between Activities in Android
what is LinearLayout,RelativeLayout and TableLayout How to Create Android Menus How to use ListView in Android