How to create text file in Android

  • by
textfile2

How to create text file in Android

A simple example on creating text files in Android.
First create a new Android project in Android studio.

DOWNLOAD SOURCE CODE FOR THIS APP.

Create a new Layout XML file with name notepad.xml in res>layout>notepad.xml.
And edit it as following :

<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:background="#222222"
android:orientation="vertical" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="SIMPLE NOTEPAD"
android:textColor="#FF8866"
android:textSize="25sp"
android:textStyle="bold"
android:typeface="monospace" />

<EditText
android:id="@+id/contenttxt"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"
android:textColor="#000000"
android:layout_marginTop="10dp"
android:gravity="top|left"
android:scrollHorizontally="false"
android:singleLine="false"
android:text="Hi" />

<Button
android:id="@+id/exportbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:background="#FF6644"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:text="EXPORT TO SDCARD"
android:textColor="#FFFFFF"
android:textSize="20sp" />

<TextView
android:id="@+id/resulttxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text=""
android:textColor="#ffffff"
android:textSize="20sp"
android:textStyle="bold"
android:typeface="monospace" />

</LinearLayout>

textfile1

Now make a new class (.java) file with name NotePad.java and edit it as following :

package app.mysqlapp;

/**
* Created by parallelcodes on 17-Jun-15.
*/

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class NotePad extends Activity {
EditText ed;
Button btn;
TextView result;
String h;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notepad);
ed = (EditText) findViewById(R.id.contenttxt);
result = (TextView) findViewById(R.id.resulttxt);
btn = (Button) findViewById(R.id.exportbtn);

btn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
try {
h = DateFormat.format("MM-dd-yyyyy-h-mmssaa", System.currentTimeMillis()).toString();
// this will create a new name everytime and unique
File root = new File(Environment.getExternalStorageDirectory(), "Notes");
// if external memory exists and folder with name Notes
if (!root.exists()) {
root.mkdirs(); // this will create folder.
}
File filepath = new File(root, h + ".txt"); // file path to save
FileWriter writer = new FileWriter(filepath);
writer.append(ed.getText().toString());
writer.flush();
writer.close();
String m = "File generated with name " + h + ".txt";
result.setText(m);

} catch (IOException e) {
e.printStackTrace();
result.setText(e.getMessage().toString());
}

}
});

}
}

Now you will have to add the permission for our app to read and write memory in AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="app.mysqlapp" >

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

<activity
android:name=".NotePad"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

textfile2


Comment below if you have any questions or just type Thanks to let me know, this post was useful to you.

DOWNLOAD SOURCE CODE FOR THIS APP.


Tags:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.