Search code examples
androidpreference

How to set a text to a Preference


How can I set text (version name, I know the code for that) to a Preference in PreferenceActivity?

String versionName = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;

Solution

  • You can do as Funkystein suggests to set the preference from the PreferenceActivity using findPreference(). Which is probably the best route. Or depending on where you are, call from any actvity

      PreferenceManager.getDefaultSharedPreferences(context).edit().putString("<PREFERENCE NAME>", versionName).commit();
    

    Where "<PREFERENCE NAME>" is the string name of the preference you are trying to change. THen when you load the preferenceactivity, it will be set.