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
    • 147
    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.

    public class MainActivity extends Activity {
          EditText editText;
         TextView textView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            editText =(EditText)findViewById(R.id.edittext);
            textView =(TextView)findViewById(R.id.textview);
            textView.setVisibility(View.GONE);
        }
    
        public void writeMessage(View view)
        {
        String Message = editText.getText().toString();
            String file_name ="hello_file";
            try{
                FileOutputStream fileOutputStream =openFileOutput(file_name,MODE_PRIVATE);
                fileOutputStream.write(Message.getBytes());
                fileOutputStream.close();
                Toast.makeText(getApplicationContext(),"Message Saved",Toast.LENGTH_LONG).show();
                editText.setText("");
    
    
            }catch (FileNotFoundException e){
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
        public void readMessage(View view)
        {
            try{
               String Message;
                FileInputStream fileInputStream = openFileInput("hello_file");
                InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
                BufferedReader bufferedReader =new BufferedReader(inputStreamReader);
                StringBuffer stringBuffer =new StringBuffer();
                while((Message=bufferedReader.readLine())!=null)
                {
                    stringBuffer.append(Message +"\n");
    
                }
                textView.setText(stringBuffer.toString());
                textView.setVisibility(View.VISIBLE);
    
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
    
        }
    
    
    
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
    
            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: