Search code examples
androidpreferenceseditbox

How to get the number value from preferences editbox in android


I have a edit box in my preference where the user can type a number to increment a counter by but I am not sure how to get this value and use it in my main class.

in my preference I have;

        android:defaultValue="1"
        android:inputType="number"
        android:key="incrementBy"
        android:maxLength="3"
        android:summary="Set the incremental value of the counter"
        android:title="Increase Count by" />

what I want to do is, get the user input from this edit box and imply it to a variable in my main class.


Solution

  • you have to get your SharedPreferences object, and from that retrieve the value of "incrementBy"

    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(yourActivity);
    String incrementBy= sharedPref.getString("incrementBy", "defaultValue");