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
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>
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.
Related
such as:
strings.xml:
<resource>
<string name="my_title">Train Video</string>
</resources>
i use this string in Java code many cases, such as:
String title = resources.getString(R.string.my_title) // title="Train Video"
textView.setText(title)
Now i get the new string valued "Train Video Demo" from server, i want to update the strings value so that when i using the following code to get new string:
String title = resources.getString(R.string.my_title) // title="Train Video Demo"
if i can't change Java code, how can i achieve it?
i know Facebook Android App can do it dynamically, i don't know it's program. Is there some blogs to explain this?
so you want to change value dynamically for that you can add %1$s in your string file
string.xml
<resource>
<string name="my_title">Title: %1$s</string>
</resources>
Activity.kt
resources.getString(R.string.my_title, title)
Here the title is dynamic value can be from API or static list.
you can't change the values in strings.xml, so you should just start off by getting all the values you need from the server to begin with, then you can cache those by using a database, you could even use a pre-populated database when releasing your app, then read those values out of the db when you need them. to update your values, make an api call, check if there are new values available, update your local db, app continues to work as normal
You can set the value of textview directly to the string you got.
textView.setText("Train Video Demo")
Since i didn't find anything usefull, or something that reply at my question i would like to know if it's possible to check if a sharedpreference key contains a constant string.
Ex.
Stored on my shared prefs:
<string name="aaa_key1">Value</string>
<string name="aaa_key2">Value</string>
<string name="aaa_key3">Value</string>
<string name="bbb_key1">Value</string>
<string name="bbb_key2">Value</string>
<string name="bbb_key3">Value</string>
I need to add a check so:
if prefs contains aaa: do something,
if prefs contains bbb: do something else.
Edit for explain:
I got some methods on my app that generate sharedpreferences keys+strings based on Users action. All the keys got a constant based on the action executed by the users, so that i need to call some other methods if the keys contains the constant (i.e aaa_key1 or bbb_key1)
Is that possible?
Thanks in advance
You may find this helpful,to get a list of keys use following method.
What you can do is use getAll() method of SharedPreferences and
get all the values in Map and then you can easily iterate through.
Map<String,?> keys = prefs.getAll();
for(Map.Entry<String,?> entry : keys.entrySet()){
Log.d("map values",entry.getKey() + ": " +
entry.getValue().toString());
}
For more info check this original post link
If you just want separate values no need to get them all - the correct answer in this case is prefs.contains(String)
if (prefs.contains("aaa")
// aaa
else if (prefs.contains("bbb")
// bbb
I want to make my app settings with multi language support. Value of settings item will be different in each language. I have string array:
<string-array name="syncTemperature">
<item>#string/celcius</item>
<item>#string/fehrenheit</item>
</string-array>
Which is used in:
<ListPreference
android:key="prefTempUnit"
android:entries="#array/syncTemperature"
android:summary="#string/pref_temp_current"
android:entryValues="#array/syncTemperature"
android:title="#string/pref_temperature" />
and when I will call:
String celcius = sharedPrefs.getString("prefTempUnit", "Celcius")
I will get different value everytime.
My question is how to have one value for all strings under one item.
For example when I want to check what user choose and make some action after.
Like this:
if(prefTemUnit==celcius){
setTempUnitToCelc();
}
EDIT:
For now I figured out one option:
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
String unit=sharedPrefs.getString("prefTempUnit", "Celcius");
String[] stringArray = getResources().getStringArray(R.array.syncTemperature);
if(unit.equals(stringArray[0])){
//mymethod
}
but I dont know if its the proper one.
Okay I got your solution:
First of all:
String celcius = sharedPrefs.getString("prefTempUnit", "Celcius") will return "Celcius" as a default value. Maybe you should remove it?
Second:
If you would like to access the String value according to the actual language, you should use:
String retrievedValueFromStringXML = getResources().getString(R.string.celcius);
To add:
ListPreference has a method called getValue(), you should use it as well to retrieve the actual value.
Everybody knows that if we have:
ekran3.setText("VAT Tax:");
We may (or even we SHOULD) convert it to:
ekran3.setText(getString(R.string.kwotaVat));
and add in strings.xml:
<string name="kwotaVat">VAT Tax:</string>
But is there some kind of trick to do it automatically? For example by clicking RMB on text and selecting some option? It would be nice to know it in fact it will save us a lot of time than while we're doing it manually.
If you are using Eclipse you may extract the string directly into the strings.xml file by placing the mouse within the string and hitting Ctrl + 1. It will bring up the dialog as followed and you may select "Extract String". You then give it a name (Ex: kwotaVat) and you're done.
hey you do not need to use getString() to convert it to string the values xml file is already having data in string form so you just need to use the following code to set the string
ekran3.setText(R.string.kwotaVat);
where ekran3 is the object of your text view
and kwotaVat is the id of your value string
for more detail od android codes have look here http://grabcodes.blogspot.com/
How to make Preferences items with programmatically filled values (like: Version, Model number...)?
Why not use android.preference.Preference directly?
<Preference
android:key="version"
android:title="#string/version" />
No edit dialog, no more attribute, and information only!!
//Set the version string in your code
findPreference("version").setSummary(version);
I accomplished this with the following:
In my application_preferences.xml I have the following preference. This makes it so it is still black (not gray), but still cannot be edited by the user. Making it gray makes it difficult to read.
<EditTextPreference
android:key="version"
android:title="#string/version"
android:enabled="false"
android:selectable="false"
android:persistent="false"
android:shouldDisableView="false"/>
In the preference activity onCreate method, get the version preference and set the title to the application version.
EditTextPreference versionPref = (EditTextPreference)findPreference("version");
String version = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
versionPref.setTitle(getString(R.string.version) + ": " + version);
You can set the preferences within you code, and check if it already is filled before you fill it. As long as you do not give users access to change this information, all they can do is "clear local data", which will cause the program to re-fill the data if you do it right.
I use this technique to store unique IDs for the device, who is logged in, etc. The user doesn't even (and should never) know what I keep track of, all they know is they have a smooth program that does what they need it to do.
Example:
SharedPreferences settings = context.getSharedPreferences("preferanceName", 0);
SharedPreferences.Editor editor = settings.edit();
int value = foo;
editor.putInt("ValueToStore", value);
editor.commit();
Kevin Westwood it is perfect thanks!!
In my case I just change the setTitle to setSummary, so the value is visible bellow the title.
String serverUrl = "http://...";
EditTextPreference pref = (EditTextPreference)findPreference("serverPrefKey");
pref.setSummary(serverUrl);