PreferenceActivity: save value as integer

androidx preference
edittextpreference
sharedpreferences
edittextpreference inputtype
android preference dependency
android preference get value
edittextpreference default value
setonpreferencechangelistener

Using a simple EditTextPreference in my preferences activity:

<EditTextPreference
    android:key="SomeKey"
    android:title="@string/some_title"
    android:summary="..."
    android:numeric="integer"
    android:maxLength="2"
/>

Is there a way that this configuration value would be saved as integer? Seems now it just allows to enter numbers, but the value is still saved as string:

Calling:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
int value = preferences.getInt("SomeKey", -1);

throws me java.lang.ClassCastException: java.lang.String, and:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String value = preferences.getString("SomeKey", "-1");

retrieves the value successfully.

How to make PreferenceActivity to save value as integer by default?


You could extend EditTextPreference:

public class IntEditTextPreference extends EditTextPreference {

    public IntEditTextPreference(Context context) {
        super(context);
    }

    public IntEditTextPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public IntEditTextPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected String getPersistedString(String defaultReturnValue) {
        return String.valueOf(getPersistedInt(-1));
    }

    @Override
    protected boolean persistString(String value) {
        return persistInt(Integer.valueOf(value));
    }
}

It would be better to overwrite onSetInitialValue() and setText() methods, but then you would have to copy some code from a base class. Above solution is simplier, but it's quite tricky - "string" methods do something with ints. Try to not extend this class further ;-)

You could use it from XML by:

<package.name.IntEditTextPreference
    android:key="SomeKey"
    android:title="@string/some_title"
    android:summary="..."
    android:numeric="integer"
    android:maxLength="2"
/>

Preference, and add a conversion from string to float if the value has been converted to a string by the (stupid) preference UI ? Note : I can provide a patch (� May be a string value, using '\\;' to escape characters such as '\ ' or '\\uxxxx' for a unicode character. May be an integer value, such as "100". May be a boolean value, either "true" or "false". May be a floating point value, such as "1.2". This corresponds to the global attribute resource symbol defaultValue.


Even if you set android:numeric="integer" it'll be text preference - as its name suggest. You could easily convert string value to int using Integer.valueOf(). Also you could overwrite PreferenceActivity to do conversion automatically on exit.


I think the best solution is to write simple method to get this value from preferences. Something like:

public static int getSomePref(Context context) {
    SharedPreferences prefs =
        PreferenceManager.getDefaultSharedPreferences(context);
    String value = prefs.getString("SomeKey", null);
    return value == null ? -1 : Integer.valueOf(value);
}

Then you could very easily use it from your code.

Better handling of float/integer in preferences � Issue #503, When using a numerical value as the defaultValue attribute in an the preference value associated with the key should be an int rather than a String it by subclassing EditTextPreference to make it save int values instead of� You should use this to save primitive data in key-value pairs. You have a key, which must be a String, and the corresponding value for that key, which can be one of: boolean, float, int, long or


Even though an Answer has been parked accepted I would like to share one more shorter way to achieve this :

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
int value = Integer.parseInt(preferences.getString("SomeKey", "-1"));

Since you have already set that only numbers can be entered this won't through any exception. yet to complete my answer :

<EditTextPref
    android:key="SomeKey"
    android:title="@string/some_title"
    android:summary="..."
    android:numeric="integer"
    android:maxLength="2" />

Crash When Using EditTextPreference with Numerical Default , It will save a long value in a preference editor. 5. putInt(String key, int value). It will save a integer value in a preference editor. 6. putFloat(String key, float value). In this video i have explained, how we can Animate the integer values in the TextView with the help of ValueAnimator in android studio.


I had the same Problem. (I wanted SharedPreference to give me a port number that i stored in a preferences xml file as defaultValue).

Implementing all the SharedPreferences methods would be much effort, so writing a custom method in the class that instanced the SharedPreferences, as broot suggested would be best i think.

You can aswell just use the Static method of Integer in the line where you need it:

int number = Integer.valueOf(settings.getString("myNumberString", "0"));

Android - Shared Preferences, I want to save and fetch the static integer value of Snow Density in Shared Preferences and change when user select another value in the Single choice. You can save a value for the setting at any time by calling one of the Preference class's persist*() methods, such as persistInt() if the setting's value is an integer or persistBoolean() to save a boolean. Note: Each Preference can save only one data type, so you must use the persist*() method appropriate for the data type used by your custom


I think this is the shortest one I could come up with:

int CheckInterval = Integer.parseInt(sharedPreferences.getString("check_frequency","60"));

How to save and fetch integer value in Shared Preference in android , The value saved in SharedPreferences for each setting can be one of the following data types: Boolean; Float; Int; Long; String; String Set. Because your app's settings UI is built using Preference objects instead of View objects, you need to� PreferenceActivity: save value as integer. Ask Question Asked 9 years, 10 months ago. Active 1 year, 4 months ago. Viewed 41k times 49. 20. Using a


Settings, The Preference API provides functions to store and retrieve small pieces of data used for application preferences. Gets an integer value from the preference. Q&A for Ubuntu users and developers. Stack Exchange Network. Stack Exchange network consists of 177 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.


Tizen Native API: Preference, However, parsing and type conversions of these default values can lead to unexpected and with an EditTextPreference (e.g., saved as res/xml/prefs.xml ): A default value for a Preference can be set using the android:defaultValue attribute: 2147483648, -2147483648, overflow (max. int value + 1).


Beware of Preference Default Values in XML � room-15, a introduction into the Java Preference API to save key/value pairs for a Java application. The value can be a boolean, string, int of another primitive type.