0

I'm new to this but I'm trying to take user input (i.e. userName) and then display it in a different activity via a textView (i.e. display will show "it's 'userName's move").

I'm not sure if I can do this by assigning the user input to a string in the java file, then passing the data with my intent or if I have to use some form of storage.

either way, I'm still confused as to how I can get this back to a string in the .xml file to be displayed.

Any help would be much appreciated :) cheers

  • 2
    would you just like to display the username on a different activity? or would you also want it to be saved to the app?, meaning even if you close the app, when you go back, the username is still there.. What have you tried so far? – omi0301 Nov 08 '12 at 04:21
  • It is possible. You can pass the string with an intent. Refer to this link. http://stackoverflow.com/questions/4114443/android-use-an-intent-to-send-data-to-my-activity, http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android . Similar questions are asked there, too. – MysticMagicϡ Nov 08 '12 at 04:23

2 Answers2

1

As it was pointed in the comments, you should understand the what an Intent is. You can read about them here. As a very brief example:

Intent launchNewActivity = new Intent(this, NewActivity.class);
launchNewActivity.putExtra("Some key", "Some value"); //sometimes you want to pass extra data
startActivity(launchNewActivity);

Also, it would be good too to understand the use of strings.xml, as explained here. Basically, it is used to define constants that will not be hard-coded and you can call in your code. They help you to keep your code organized and also helps to translate your applications to other languages with ease. Again, a simple example is shown below:

<resources>

    <string name="OK">OK</string>
    <string name="cancel">Cancel</string>

    <!-- Validation error messages for EditText -->
    <string name="editText_validation_error_empty_field">The field cannot be empty.</string>
    <string name="editText_validation_error_numbers_only">Only numbers are allowed on this field.</string>
</resources>
MigDus
  • 767
  • 5
  • 17
1

I think the best way is to use the SHARED PREFERENCES. its exactly for things like that. thats how you do it:

SharedPreferences prefs = getSharedPreferences("prefs", 0); 
SharedPreferences.Editor editor prefs.edit();

now, every time you want to change the string you do:

editor.putString("user_name", "jon").commit();

the "user_name" is the name of the string, and the "jon" is the content. so you can save first and last name like this:

editor.putString("user_name", "jon")
editor.putString("last_name", "dow").commit();

dont foget to put the "commit()" at the end. and every time you want to get the string you can use:

String Name = prefs.getString("user_name", "");
String LastName = prefs.getString("last_name", "");

the good thing about this is that it is saved. so, the next time the user open the app you can still get the strings without making the user put it again by using this again:

String Name = prefs.getString("user_name", "");
String LastName = prefs.getString("last_name", "");

its that simple. hope you got it.

roiberg
  • 13,629
  • 12
  • 60
  • 91
  • thanks for the tip. Although maybe I should have worded my question a bit differently. I'm pretty sure this is an example of how to store data in Shared Preferences (which is good because this confirms my suspicion that this was the best way to store the info) however the real heart of my question was how to display this data once its stored? any thoughts? I was under the impression that displaying any kind of text had to be done through the xml file (using the string file so the app can change languages if need ). How do you go from java to xml then? – Jay Hennessy Dec 12 '12 at 00:31