Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Activity_main.xml
- <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:orientation="vertical"
- android:gravity="center">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- android:gravity="center">
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Create new file"
- android:id="@+id/btn_create"
- android:layout_gravity="left"/>
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Open"
- android:id="@+id/btn_open"
- android:layout_gravity="right"/>
- </LinearLayout>
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:text="File:"
- android:id="@+id/lbl_file" />
- <EditText
- android:layout_width="match_parent"
- android:layout_height="200dp"
- android:id="@+id/txt_content"/>
- <Button android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Save"
- android:layout_gravity="center"
- android:id="@+id/btn_save"/>
- </LinearLayout>
- Dialog_layout.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:orientation="vertical" >
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Enter File Name"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/txt_filename"/>
- </LinearLayout>
- Manifest.xml
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.p2"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="18" />
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.example.p2.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>
- MainActivity.java
- package com.example.p2;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import android.os.Bundle;
- import android.os.Environment;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MainActivity extends Activity implements OnClickListener {
- TextView lblFile;
- EditText txtContent;
- Button btnCreate,btnSave,btnOpen;
- int FILE_CHOOSE_REQUEST=1;
- String filepath;
- String filename;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- lblFile=(TextView)findViewById(R.id.lbl_file);
- txtContent=(EditText)findViewById(R.id.txt_content);
- btnCreate=(Button)findViewById(R.id.btn_create);
- btnCreate.setOnClickListener(this);
- btnSave=(Button)findViewById(R.id.btn_save);
- btnSave.setOnClickListener(this);
- btnOpen=(Button)findViewById(R.id.btn_open);
- btnOpen.setOnClickListener(this);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- public void onActivityResult(int requestCode,int resultCode, Intent data)
- {
- super.onActivityResult(requestCode, resultCode, data);
- if(resultCode==RESULT_OK)
- {
- filepath=data.getData().getPath();
- filename=filepath.substring(filepath.lastIndexOf("/")+1);
- filepath=filepath.substring(0,filepath.lastIndexOf("/"));
- readFromFile(filepath,filename);
- lblFile.setText(filepath+"/"+filename);
- }
- else
- {
- Toast.makeText(this,"Wrong Choice of File",
- Toast.LENGTH_LONG).show();
- }
- }
- public void writeToFile(String path,String filename)
- {
- try {
- FileOutputStream fileout=new FileOutputStream(new
- File(path+"/"+filename));
- OutputStreamWriter outputWriter=new OutputStreamWriter(fileout);
- outputWriter.write(txtContent.getText().toString());
- outputWriter.close();
- //display file saved message
- Toast.makeText(getBaseContext(), "File Saved successfully!",
- Toast.LENGTH_SHORT).show();
- } catch (Exception e) {
- Toast.makeText(getBaseContext(), e.getLocalizedMessage(),
- Toast.LENGTH_SHORT).show();
- }
- }
- public void readFromFile(String path,String filename)
- {
- try {
- FileInputStream fileIn=new FileInputStream(path+"/"+filename);
- InputStreamReader inputReader= new InputStreamReader(fileIn);
- BufferedReader br=new BufferedReader(inputReader);
- String data=br.readLine();
- while(data!=null)
- {
- txtContent.append(data);
- data=br.readLine();
- }
- br.close();
- } catch (Exception e) {
- Toast.makeText(getBaseContext(),e.getLocalizedMessage(),
- Toast.LENGTH_SHORT).show();
- }
- }
- public void onShowCreateDialog()
- {
- AlertDialog.Builder builder=new AlertDialog.Builder(this);
- final View dialogView=getLayoutInflater().inflate(R.layout.dialog_layout, null);
- builder.setView(dialogView);
- builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface arg0, int arg1) {
- // TODO Auto-generated method stub
- EditText
- txtFilename=(EditText)dialogView.findViewById(R.id.txt_filename);
- filepath=Environment.getExternalStorageDirectory().getAbsolutePath();
- filename=txtFilename.getText().toString();
- File f=new File(filepath+"/"+filename);
- try {
- f.createNewFile();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- Toast.makeText(getBaseContext(),
- ""+e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
- }
- lblFile.setText(filepath+"/"+filename);
- }
- });
- builder.setNegativeButton("Cancel", null);
- AlertDialog dialog=builder.create();
- dialog.show();
- }
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- if(v.equals(btnOpen))
- {
- Intent it=new Intent(Intent.ACTION_GET_CONTENT);
- //it.setType("*.*");
- it.setType("file/*");
- startActivityForResult(it, 0);
- }
- else if(v.equals(btnCreate))
- {
- onShowCreateDialog();
- }
- else if(v.equals(btnSave))
- {
- writeToFile(filepath, filename);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement