0

I got some help here today to make a string, which gets displayed after pressing the button:

public void addListenerOnButton() {

    button1 = (Button) findViewById(R.id.buttoncalculate);
    button1.setOnClickListener(new View.OnClickListener() {

    public void onClick(View view) {

            EditText editText = (EditText)findViewById(R.id.editText1);
            String text = editText.getText().toString();

             Intent myIntent = new Intent(view.getContext(),Calculated.class);
             myIntent.putExtra("mytext",text);
             startActivity(myIntent);

        }
    });

And it gets displayed with:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    setContentView(R.layout.calculated);

       mTextview = (TextView)findViewById(R.id.textView1);

       mTextview.setText(getIntent().getStringExtra("mytext"));
}

But i noticed that that is only temporarily. As soon as i leave the app or go to an other activity, the string gets reset..

My question is: How can i store the local string to a real string and thus display it on other activities and even after the app gets closed?

Is it something with strings.xml?

Thanks

John
  • 295
  • 1
  • 5
  • 16
  • 2
    No, it is nothing with `strings.xml`. You can't modify that at runtime. Save your String to SharedPreferences or a database. – A--C Feb 24 '13 at 17:36
  • Could you explain how to use SharedPreferences or provide a link please? – John Feb 24 '13 at 17:37

2 Answers2

1

Is it something with strings.xml?

No - This is read only and used during compile.

How can i store the local string to a real string and thus display it on other activities and even after the app gets closed?

What you are talking about is persistent storage. You have 3 main options (there are others but as this is quite a broad topic I shall keep things simple):

Store it in a file

Save the string to a file and load it when the app starts or where ever you need it.

See this SO post on reading/writing text files Android write file

Use SharedPreferences

SO Post showing code on this android read/write user preferences

Probably the best answer for storing a simple string and easy to code. If you start your Activity and there is no intent passed in then you can load the string from SharedPreferences.

mTextview.setText(getIntent().getStringExtra("mytext"));

becomes

final String myString = getSharedPreferences("PREF_NAME", Context.MODE_PRIVATE).getString("SOME_STRING", "Default if not found");

mTextview.setText(myString);

The above is purely to demonstrate and probably needs tidying up and refining for your own needs.

Use a Database

Probably the worst option for you but worth thinking about. If you begin to have more than one String or complex option then SQLite is built into Android.

A good tutorial is found here http://www.vogella.com/articles/AndroidSQLite/article.html

Community
  • 1
  • 1
Graham Smith
  • 25,627
  • 10
  • 46
  • 69
0

I prefer to use Shared Preferences to store value.

MainActivity.java

public void addListenerOnButton() {

    button1 = (Button) findViewById(R.id.buttoncalculate);
    button1.setOnClickListener(new View.OnClickListener() {

    public void onClick(View view) {

            EditText editText = (EditText)findViewById(R.id.editText1);
            String text = editText.getText().toString();

            SharedPreferences sp = getSharedPreferences("MyPrefs", MODE_PRIVATE);

            Editor editor = sp.edit();
            editor.putString("mytext", text);
            editor.commit();


             Intent myIntent = new Intent(view.getContext(),Calculated.class);
             startActivity(myIntent);

        }
    });

Calculated.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    setContentView(R.layout.calculated);

       mTextview = (TextView)findViewById(R.id.textView1);

       mTextview.setText(getSharedPreferences("MyPrefs", MODE_PRIVATE).getString("mytext",null););
}
moDev
  • 5,248
  • 4
  • 33
  • 63
  • Thanks, you helped me out again! I'll mark this answer as best answer, as the other didn't work for me.. – John Feb 24 '13 at 17:49