-2

In this app, I want to save the text that ends up on the TextView view and be able to see it the next time I launch the app. I'm relatively new to Android Studio. How can I do this? My code:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {


Button submit;
EditText habbit;
EditText money;
TextView view;


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


     submit = (Button) findViewById(R.id.button2);
     habbit = (EditText) findViewById(R.id.habbit1);
      money = (EditText)findViewById(R.id.money1);
      view = (TextView)findViewById(R.id.textView3);

    submit.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            view.setText((habbit.getText().toString() + " for $" + money.getText().toString()) + "/day");

        }
    });

}}
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141

2 Answers2

3

You can use shared preferences to save the text in a file. And when app is open again you can retrieve the text from file and put in a textView.

public class MainActivity extends AppCompatActivity {

    SharedPreferences preferences;
    SharedPreferences.Editor editor;


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

        preferences=getSharedPreferences("File_name",MODE_PRIVATE);
        editor=preferences.edit();

        submit.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                view.setText((habbit.getText().toString() + " for $" + money.getText().toString()) + "/day");

                editor.putString("key",habbit.getText().toString());
                editor.commit();

            }
        });
}

And then you can retrieve data as -

public class MainActivity extends AppCompatActivity {

    SharedPreferences preferences;

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

        preferences=getSharedPreferences("File_name",MODE_PRIVATE);

        String data = preferences.getString("key", "");
        view = (TextView)findViewById(R.id.textView3);
        view.setText(data);
    }
}
Raj
  • 2,997
  • 2
  • 12
  • 30
  • I hope it will do your job @Juan Francisco Patino. – Raj Jul 22 '18 at 18:03
  • it did, thanks! The only thing is that it only saved the 'habbit' text but I fixed it to it save both "habbit" and "money". I also realized how horrible my spelling of 'habit' is. Thanks so much – Juan Francisco Patino Jul 22 '18 at 18:16
0

You can use shared preferences to store a simple string.

static final String MY_PREF = "MY_PREF";
static final String SAVE_TEXT = "SAVE_TEXT";
SharedPreferences sharedPreferences = context.getSharedPreferences(MY_PREF, Context.MODE_PRIVATE);

//To store string use this line
sharedPreferences.edit().putString(SAVE_TEXT, textview.getText().toString()).commit();

//To retrieve the same use this line
sharedPreferences.getString(SAVE_TEXT, "default_value");
Shivam Pokhriyal
  • 1,044
  • 11
  • 26