Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Save Data in Android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 160
    Comment on it

    In below example I have created a save data app code in android. In this save data we can save our data and then load the back-up. In this I have also described how to make save data.

    1. public class MainActivity extends Activity {
    2. EditText editText;
    3. TextView textView;
    4.  
    5. @Override
    6. protected void onCreate(Bundle savedInstanceState) {
    7. super.onCreate(savedInstanceState);
    8. setContentView(R.layout.activity_main);
    9. editText =(EditText)findViewById(R.id.edittext);
    10. textView =(TextView)findViewById(R.id.textview);
    11. textView.setVisibility(View.GONE);
    12. }
    13.  
    14. public void writeMessage(View view)
    15. {
    16. String Message = editText.getText().toString();
    17. String file_name ="hello_file";
    18. try{
    19. FileOutputStream fileOutputStream =openFileOutput(file_name,MODE_PRIVATE);
    20. fileOutputStream.write(Message.getBytes());
    21. fileOutputStream.close();
    22. Toast.makeText(getApplicationContext(),"Message Saved",Toast.LENGTH_LONG).show();
    23. editText.setText("");
    24.  
    25.  
    26. }catch (FileNotFoundException e){
    27. e.printStackTrace();
    28. } catch (IOException e) {
    29. e.printStackTrace();
    30. }
    31.  
    32. }
    33. public void readMessage(View view)
    34. {
    35. try{
    36. String Message;
    37. FileInputStream fileInputStream = openFileInput("hello_file");
    38. InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
    39. BufferedReader bufferedReader =new BufferedReader(inputStreamReader);
    40. StringBuffer stringBuffer =new StringBuffer();
    41. while((Message=bufferedReader.readLine())!=null)
    42. {
    43. stringBuffer.append(Message +"\n");
    44.  
    45. }
    46. textView.setText(stringBuffer.toString());
    47. textView.setVisibility(View.VISIBLE);
    48.  
    49. } catch (FileNotFoundException e) {
    50. e.printStackTrace();
    51. } catch (IOException e) {
    52. e.printStackTrace();
    53. }
    54.  
    55.  
    56. }
    57.  
    58.  
    59.  
    60.  
    61. @Override
    62. public boolean onCreateOptionsMenu(Menu menu) {
    63. // Inflate the menu; this adds items to the action bar if it is present.
    64. getMenuInflater().inflate(R.menu.menu_main, menu);
    65. return true;
    66. }
    67.  
    68. @Override
    69. public boolean onOptionsItemSelected(MenuItem item) {
    70. // Handle action bar item clicks here. The action bar will
    71. // automatically handle clicks on the Home/Up button, so long
    72. // as you specify a parent activity in AndroidManifest.xml.
    73. int id = item.getItemId();
    74.  
    75. //noinspection SimplifiableIfStatement
    76. if (id == R.id.action_settings) {
    77. return true;
    78. }
    79.  
    80. return super.onOptionsItemSelected(item);
    81. }
    82. }

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: