Code of a simple android app which can browse and open a text file

Here I provide the codes of a simple android app which can be used to browse and open a text file from device.

Codes in main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Button"
android:id="@+id/select"/>
<EditText
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:ems="10"
android:id="@+id/edittext1"/>
<EditText
android:layout_height="match_parent"
android:layout_width="match_parent"
android:ems="10"
android:id="@+id/edittext2"/>
</LinearLayout>

Codes in MainActivity.java
package com.myfile.open;

import android.app.*;
import android.os.*;
import android.widget.*;
import android.view.*;
import android.view.View;
import android.content.*;
import java.io.*;

public class MainActivity extends Activity 
{
private Button btn;
private EditText edittext1;
private EditText edittext2;
private static final int PICKFILE_RESULT_CODE = 1;

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

btn = (Button) findViewById(R.id.select);
edittext1 = (EditText) findViewById(R.id.edittext1);
edittext2 = (EditText) findViewById(R.id.edittext2);

btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("text/*");
startActivityForResult(intent,PICKFILE_RESULT_CODE);}
});
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK) {
switch(requestCode) {
case PICKFILE_RESULT_CODE:

String selectedFile = data.getData().getPath();
String filename= selectedFile.substring(selectedFile.lastIndexOf("/")+1);
edittext1.setText(filename);
selectedFile = selectedFile.substring(selectedFile.lastIndexOf(":")+1);

try {
File myFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + selectedFile);
FileInputStream fIn = new FileInputStream( myFile );
BufferedReader myReader = new BufferedReader( new InputStreamReader(fIn));
String aDataRow = "";
String aBuffer = "";
while ((aDataRow = myReader.readLine()) != null) { aBuffer+= aDataRow + "\n";}
edittext2.setText(aBuffer);
myReader.close();
Toast.makeText(getBaseContext(),"Done reading file "+filename+" from sdcard",Toast.LENGTH_SHORT).show();}
catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),Toast.LENGTH_SHORT).show();}

break;
         }
      }
   }

}

Codes in AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android= "http://schemas.android.com/apk/res/android"
package= "com.myfile.open" >
<uses-permission android:name= "android.permission.WRITE_EXTERNAL_STORAGE" />
<application
     android:allowBackup="true"
     android:icon="@drawable/ic_launcher"
     android:label="@string/app_name"
     android:theme="@style/AppTheme"
     android:resizeableActivity = "true">
     <activity
         android:name=".MainActivity"
         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>

Note that this app requires permissions to WRITE EXTERNAL STORAGE.

The code has been described in this video:

Komentar

Cara pembuatan Apps paling Populer

Create Stopwatch App in Android using Sketchware

How to enable download in webview in Sketchware apps?

TextInputLayout in Sketchware

Create Stopwatch App in Android using Sketchware

How to integrate Admob Ads in Sketchware project using AIDE?

How to find and​ highlight a word in a text field in Sketchware?

Code for implementing Notifications in Sketchware

A Flash Light App in Sketchware

Create a Stopwatch App using Chronometer in Sketchware

Create app to save all passwords, protected using voice key