Advertisement
GGGG2468

Save Open Create

Jan 30th, 2023
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.06 KB | None | 0 0
  1. Activity_main.xml
  2.  
  3.  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5. xmlns:tools="http://schemas.android.com/tools"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:orientation="vertical"
  9. android:gravity="center">
  10.  
  11. <LinearLayout
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:orientation="horizontal"
  15. android:gravity="center">
  16.  
  17. <Button
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:text="Create new file"
  21. android:id="@+id/btn_create"
  22. android:layout_gravity="left"/>
  23.  
  24. <Button
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:text="Open"
  28. android:id="@+id/btn_open"
  29. android:layout_gravity="right"/>
  30.  
  31. </LinearLayout>
  32. <TextView
  33. android:layout_width="fill_parent"
  34. android:layout_height="wrap_content"
  35. android:gravity="center"
  36. android:text="File:"
  37. android:id="@+id/lbl_file" />
  38.  
  39. <EditText
  40. android:layout_width="match_parent"
  41. android:layout_height="200dp"
  42. android:id="@+id/txt_content"/>
  43.  
  44. <Button android:layout_width="wrap_content"
  45. android:layout_height="wrap_content"
  46. android:text="Save"
  47. android:layout_gravity="center"
  48. android:id="@+id/btn_save"/>
  49. </LinearLayout>
  50.  
  51.  
  52.  
  53. Dialog_layout.xml
  54.  
  55.  
  56.  
  57. <?xml version="1.0" encoding="utf-8"?>
  58. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  59. android:layout_width="match_parent"
  60. android:layout_height="match_parent"
  61. android:orientation="vertical" >
  62.  
  63. <TextView
  64. android:layout_width="match_parent"
  65. android:layout_height="wrap_content"
  66. android:text="Enter File Name"/>
  67.  
  68. <EditText
  69. android:layout_width="match_parent"
  70. android:layout_height="wrap_content"
  71. android:id="@+id/txt_filename"/>
  72.  
  73. </LinearLayout>
  74.  
  75.  
  76. Manifest.xml
  77.  
  78.  
  79.  
  80. <?xml version="1.0" encoding="utf-8"?>
  81. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  82. package="com.example.p2"
  83. android:versionCode="1"
  84. android:versionName="1.0" >
  85. <uses-sdk
  86. android:minSdkVersion="8"
  87. android:targetSdkVersion="18" />
  88. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  89. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
  90. <application
  91. android:allowBackup="true"
  92. android:icon="@drawable/ic_launcher"
  93. android:label="@string/app_name"
  94. android:theme="@style/AppTheme" >
  95. <activity
  96. android:name="com.example.p2.MainActivity"
  97. android:label="@string/app_name" >
  98. <intent-filter>
  99. <action android:name="android.intent.action.MAIN" />
  100. <category android:name="android.intent.category.LAUNCHER" />
  101. </intent-filter>
  102. </activity>
  103. </application>
  104. </manifest>
  105.  
  106.  
  107.  
  108. MainActivity.java
  109.  
  110.  
  111.  
  112. package com.example.p2;
  113. import java.io.BufferedReader;
  114. import java.io.File;
  115. import java.io.FileInputStream;
  116. import java.io.FileOutputStream;
  117. import java.io.IOException;
  118. import java.io.InputStreamReader;
  119. import java.io.OutputStreamWriter;
  120. import android.os.Bundle;
  121. import android.os.Environment;
  122. import android.app.Activity;
  123. import android.app.AlertDialog;
  124. import android.content.DialogInterface;
  125. import android.content.Intent;
  126. import android.view.Menu;
  127. import android.view.View;
  128. import android.view.View.OnClickListener;
  129. import android.widget.Button;
  130. import android.widget.EditText;
  131. import android.widget.TextView;
  132. import android.widget.Toast;
  133. public class MainActivity extends Activity implements OnClickListener {
  134. TextView lblFile;
  135. EditText txtContent;
  136. Button btnCreate,btnSave,btnOpen;
  137. int FILE_CHOOSE_REQUEST=1;
  138. String filepath;
  139. String filename;
  140. @Override
  141. protected void onCreate(Bundle savedInstanceState) {
  142. super.onCreate(savedInstanceState);
  143. setContentView(R.layout.activity_main);
  144. lblFile=(TextView)findViewById(R.id.lbl_file);
  145.  
  146. txtContent=(EditText)findViewById(R.id.txt_content);
  147.  
  148. btnCreate=(Button)findViewById(R.id.btn_create);
  149. btnCreate.setOnClickListener(this);
  150.  
  151. btnSave=(Button)findViewById(R.id.btn_save);
  152. btnSave.setOnClickListener(this);
  153.  
  154. btnOpen=(Button)findViewById(R.id.btn_open);
  155. btnOpen.setOnClickListener(this);
  156. }
  157.  
  158.  
  159. @Override
  160. public boolean onCreateOptionsMenu(Menu menu) {
  161. // Inflate the menu; this adds items to the action bar if it is present.
  162. getMenuInflater().inflate(R.menu.main, menu);
  163. return true;
  164. }
  165.  
  166. public void onActivityResult(int requestCode,int resultCode, Intent data)
  167. {
  168. super.onActivityResult(requestCode, resultCode, data);
  169. if(resultCode==RESULT_OK)
  170. {
  171. filepath=data.getData().getPath();
  172. filename=filepath.substring(filepath.lastIndexOf("/")+1);
  173. filepath=filepath.substring(0,filepath.lastIndexOf("/"));
  174. readFromFile(filepath,filename);
  175. lblFile.setText(filepath+"/"+filename);
  176. }
  177. else
  178. {
  179. Toast.makeText(this,"Wrong Choice of File",
  180. Toast.LENGTH_LONG).show();
  181. }
  182. }
  183. public void writeToFile(String path,String filename)
  184. {
  185. try {
  186. FileOutputStream fileout=new FileOutputStream(new
  187. File(path+"/"+filename));
  188. OutputStreamWriter outputWriter=new OutputStreamWriter(fileout);
  189. outputWriter.write(txtContent.getText().toString());
  190. outputWriter.close();
  191. //display file saved message
  192. Toast.makeText(getBaseContext(), "File Saved successfully!",
  193. Toast.LENGTH_SHORT).show();
  194. } catch (Exception e) {
  195. Toast.makeText(getBaseContext(), e.getLocalizedMessage(),
  196. Toast.LENGTH_SHORT).show();
  197. }
  198. }
  199.  
  200.  
  201. public void readFromFile(String path,String filename)
  202. {
  203. try {
  204. FileInputStream fileIn=new FileInputStream(path+"/"+filename);
  205. InputStreamReader inputReader= new InputStreamReader(fileIn);
  206. BufferedReader br=new BufferedReader(inputReader);
  207. String data=br.readLine();
  208. while(data!=null)
  209. {
  210. txtContent.append(data);
  211. data=br.readLine();
  212. }
  213. br.close();
  214. } catch (Exception e) {
  215. Toast.makeText(getBaseContext(),e.getLocalizedMessage(),
  216. Toast.LENGTH_SHORT).show();
  217. }
  218. }
  219.  
  220. public void onShowCreateDialog()
  221. {
  222. AlertDialog.Builder builder=new AlertDialog.Builder(this);
  223. final View dialogView=getLayoutInflater().inflate(R.layout.dialog_layout, null);
  224. builder.setView(dialogView);
  225. builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
  226. @Override
  227. public void onClick(DialogInterface arg0, int arg1) {
  228. // TODO Auto-generated method stub
  229. EditText
  230. txtFilename=(EditText)dialogView.findViewById(R.id.txt_filename);
  231. filepath=Environment.getExternalStorageDirectory().getAbsolutePath();
  232. filename=txtFilename.getText().toString();
  233. File f=new File(filepath+"/"+filename);
  234. try {
  235. f.createNewFile();
  236. } catch (IOException e) {
  237. // TODO Auto-generated catch block
  238. Toast.makeText(getBaseContext(),
  239. ""+e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
  240. }
  241. lblFile.setText(filepath+"/"+filename);
  242. }
  243. });
  244. builder.setNegativeButton("Cancel", null);
  245. AlertDialog dialog=builder.create();
  246. dialog.show();
  247. }
  248. @Override
  249. public void onClick(View v) {
  250. // TODO Auto-generated method stub
  251. if(v.equals(btnOpen))
  252. {
  253. Intent it=new Intent(Intent.ACTION_GET_CONTENT);
  254. //it.setType("*.*");
  255. it.setType("file/*");
  256. startActivityForResult(it, 0);
  257. }
  258. else if(v.equals(btnCreate))
  259. {
  260. onShowCreateDialog();
  261. }
  262. else if(v.equals(btnSave))
  263. {
  264. writeToFile(filepath, filename);
  265. }
  266. }
  267.  
  268. }
  269.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement