I am trying to create preferences for an experimental app for android. However I can get the preferences to work.
The preference that I want to use determine the display langue and use this function to retrieve it:
private String selectLang()
{
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String lang = preferences.getString("lang", "n/a");
return lang;
}
Then inside onCreate method for the ListActivity I use this code to display the right language:
String lang = selectLang();
String title = (lang == "english") ? "English" : "Arabic";
title is a column in the database. The code does not seem to work and I get Arabic no matter what. I used this code just to know the value of lang:
Toast.makeText(this, lang, Toast.LENGTH_LONG).show();
and I get “english” but the English language does not display.
Here is is my XML for the list:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="lang">
<item>Arabic</item>
<item>English</item>
</string-array>
<string-array name="langValues">
<item>arabic</item>
<item>english</item>
</string-array>
</resources>
I have read in number of answers here on SO that I should use SharedPreference editor but I could not get a full working example of that.
Can you please help?
This:
lang == "english"should belang.equals("english").Compare strings (as any other object) using
equalsorequalsIgnoreCase. The==operator compares object references, not content of objects, so your check always evaluated as false.