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
Related
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.
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.
I would like to include the UserName in the AppBar label / title area at the top of the layout. To my understanding res\values\strings.xml is a great place to put some constant values like the AppName. So I have
<resources>
<string name="app_name">MyFirstApp</string>...
In the manifest I have #string/app_name for the Label field.
So how do I go about getting the username added to the label?
I thought about creating a second string res constant named str_UserName. The value would be blank until authentication then programatically update str_UserName after succesful login. The trouble I have there is concatenating in the manifest. It doesn't like any combination I come up with. (for example #string/app_name + #string/str_UserName or #string/app_name + str_UserName etc.)
Then Googling I read up on string arrays. But in my attempts to use a string array it apears to not properly work for the manifest. I'm assuming because you can't just reference the array without and indexer for which element to display??
So in the end what I want is for the App Name to be left justified and preferrably use a constant value and the username be right justified and of course dynamic based upon the user.
TIA
JB
The getString method takes parameters, and works similarly to the String.format method. This is not very often used, but it is documented and works fine. So you can do something like that:
<string name="my_custom_title">MyFirstApp for user %s</string>
And, once you have your username:
setTitle(getString(R.string.my_custom_title, username));
Source: The Context API
You can't change values / strings in res/values/ and you can't concatenated them either.
But it's not required. Once you have the username:
setTitle(getString(R.string.app_name) + " " + username);
and the title will change.
I am showing data of a <string name="whatsnew"> </string> in a textview and i have to show this textview only when the text inside this being changed.
for eg:
If previous state is:
<string name="whatsnew">Hi Stackoverflow Collegues </string>
If current state is this data is being updated as:
<string name="whatsnew">Hello Stackoverflow Collegues</string>
I have implemented a logic that i will retrieve the previous and current stringlength of
<string name="whatsnew"> </string>
containing text.But this logic has a loop hole if the updated text is of same length of previous string length.
Please help me with a better and efficient logic.
use hashCode() of your string to compare, for example:
int hash = "this is my string".hashCode();
or simply compare with another string:
boolean isSame = "first string".equalsIgnoreCase("second string");
Could you just compare the strings and set it visible if they are different?
if(oldString != newString){
view.setVisibility(VIEW.Visible");
}
getText() method may help .. check if the same text is in the TextView of it changed
just compare the whole text
I want to write some values from editText of activity in the file string.xml.
I will explain with an example:
In the activity I have 2 editText and 1 button to submit the information. Imagine that the editText are for inserting NAME and SURNAME.
I have the following code in string.xml
<string name="person_name"></string>
<string name="person_surname"></string>
At the begining, those fields will be empty but after submiting the info, I want to write the values obtained from the editTexts in that file.
Any suggestion?
Thanks in advance
That's not possible. Strings.xml is only for storing predefined strings. Use SharedPreferences to store this data.
Although what you want is not possible but can be done through SharedPreferences http://developer.android.com/guide/topics/data/data-storage.html#pref