Wednesday, January 16, 2013

Create Text file in Android


Hi, To day I'm going to write "how to create a file (text file) in Android".

In Eclipse File ---> New ---> Android Project



Type the ProjectName as “CreateTextFile”  and click “Next”



Choose the SDK version for this project. I choose Android 4.0.3

Click “Next"



Type the package Name as “niro.CreateTextFile” and click “Finish”

In the Package Explorer our project will display.


Expand our project “CreateTextFile” and open the "main.xml" file which is in res/layout folder to design our UI.

In this application I add two TextView, two EditText and a button.

Type the following code in "main.xml"

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="File Name" />

    <EditText
        android:id="@+id/fileName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </EditText>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Type your Message here" />

    <EditText
        android:id="@+id/msgBody"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </EditText>

    <Button
        android:id="@+id/saveButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save" />

</LinearLayout>




We need to set permission to write a file in SD card.

To set permission, Open  “AndroidManifest.xml” file  and add the following code

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

Then open the “CreateTextFileActivity.java” file to write the code.

CreateTextFileActivity.java

package niro.CreateTextFile;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class CreateTextFileActivity extends Activity {
       final int ACTIVITY_CHOOSE_FILE = 1;
       String msg;
       String title;

       /** Called when the activity is first created. */
       @Override
       public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
              Button btn = (Button) this.findViewById(R.id.saveButton);
              final EditText etTitle = (EditText)this.findViewById(R.id.fileName);
              final EditText etBody = (EditText) this.findViewById(R.id.msgBody);

              btn.setOnClickListener(new OnClickListener() {
                     public void onClick(View v) {
                           title = etTitle.getText().toString() + ".txt";
                           msg = etBody.getText().toString();
                           createFileOnSD(title, msg);
                     }
              });
       }

       public void createFileOnSD(String fileName, String body) {
              try {
                   File root = new File(Environment.getExternalStorageDirectory(),
                                  "MyFolder");
                     if (!root.exists()) {
                           root.mkdirs();
                     }
                     File gpxfile = new File(root, fileName);
                     FileWriter writer = new FileWriter(gpxfile);
                     writer.append(body);
                     writer.flush();
                     writer.close();
                     Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
              } catch (IOException e) {
                     e.printStackTrace();
                     // importError = e.getMessage();
                     // iError();
              }
       }


After save this program, we need to run this program. I test this application in my phone instead of emulator.


Then type the File Name as “test” and Type the message as “This is the body” and click Save button


Now our application has created a folder, name "MyFolder", and write a text file, name "test.txt" in that folder.


























Open the "test.txt" file



When we go back to home screen, our application will display on the screen.



1 comment:

  1. Excellent and very informative. Its really very useful.Thanks a lot for share it.

    android development

    ReplyDelete