Search code examples
androidlocalepreferencesandroid-preferences

Determining if an Android preference was set previously


I have an app which will have the UI text in different languages. I do not want to save this in the strings.xml, because I want the user to be able to change them (and not be dependant on the phone's locale).

I've setup a preference to read this. By default, I want the UI to take the locale value, but if the user has changed the UI lang preference, I want it to use that.

Is there any way to determine if a preference has been set previously? The thing is, the getInt makes me set a default, so I can't check (for example) that uiLang==null to determine if a preference was set before or not.

SharedPreferences settings = getPreferences(MODE_PRIVATE);
//If uiLang has not been set before, then lets use the locale.
uiLang = settings.getInt("uiLang", 1);  //English (=1) es default
myUI = new UIText(uiLang);
myTextView.setText(myUI.title);

Solution

  • you could use the 'contains' method to check this:

    http://developer.android.com/reference/android/content/SharedPreferences.html#contains%28java.lang.String%29