Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a preference that accepts only integer values

Is there a way to create a preference in a PreferenceFragment that accepts only integer values? I could implement an EditTextPreference and register an OnPreferenceChangeListener in which I could reject the change if the user enters a a string that is not a number, but I would prefer something that is meant for holding only numbers and that does not allow users to enter anything else, maybe showing only a dial pad keyboard.. I don't such a preference exist, since every descendant of Preference is mapped onto a Boolean (CheckBoxPreference), a String (*EditTextPreference) or a String array (MultiSelectListPreference), i.e. there are no preferences mapped onto integers, but maybe some of you can give me an hint or at least tell me if there are better solutions than the one I've proposed above.

Solution proposed by Grey:

EditText editText = ((EditTextPreference)    
                             findPreference("intent_property")).getEditText();
editText.setKeyListener(new NumberKeyListener() {
    @Override
    public int getInputType() {
        // The following shows the standard keyboard but switches to the view 
        // with numbers on available on the top line of chars
        return InputType.TYPE_CLASS_NUMBER;
        // Return the following to show a dialpad as the one shown when entering phone
        // numbers.
        // return InputType.TYPE_CLASS_PHONE
    }

    @Override
    protected char[] getAcceptedChars() {
        return new String("1234567890").toCharArray();
    }
});

Shorter solution, which does not allow varying the keyboard to dialpad but requires less code:

EditText editText = ((EditTextPreference) 
                             findPreference("intent_property")).getEditText();
editText.setKeyListener(new DigitsKeyListener());

I don't like just one thing about this solution: the user can enter 0000 and it is accepted and saved in the shared preference (which is a String) as "0000".. this requires you to implement a OnSharedPreferenceChangeListener to intercept changes to shared preferences and remove leading zeros in this preference or implement a change listener directly on this preference and return false to refuse numbers with trailing zeros (tell me if there is a better solution to this last problem which does not involve implementing your own Preference). It would be beautiful if we could modify the newValue in OnPreferenceChangeListener..

like image 213
Gianni Costanzi Avatar asked May 30 '12 21:05

Gianni Costanzi


4 Answers

There is an easier way to do this. Just set in the EditTextPreference in your XML to android:numeric="integer" (you can set it to integer, signed or decimal).

Eclipse (or whatever tool you are working in) won't show you this as a possible attribute, but just write it in your EditTextPreference and clean your project. You will see that it won't give you any errors. :)

like image 193
Tooroop Avatar answered Nov 02 '22 04:11

Tooroop


After reading the proposed solutions, I still think a custom preference is the way to go. I did read your remark in bold text, but setting up a basic EditTextIntegerPreference is actually super simple and it will solve the remaining issue you have when the user enters for example "0000".

Just a note up front: since you normally want to be able to use preferences in several places in your app, as far as I'm concerned, a proper implementation of an EditTextIntegerPreference would store its value against an Integer. That way you'll be able to retrieve the int value anywhere, without the need to first parse or cast it.

However, to keep this answer to the point and compact, I'm actually going to show an extension of a regular EditTextPreference, which means that under the hood, the value will still be stored as a string. If you're really keen on getting the 'proper' implementation to work, I don't mind writing that up later on. It shouldn't be too tricky though, so you might want to have a crack at it yourself first. :)

public class EditTextIntegerPreference extends EditTextPreference {

    private Integer mInteger;

    public EditTextIntegerPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        getEditText().setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED);
    }

    public EditTextIntegerPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        getEditText().setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED);
    }

    public EditTextIntegerPreference(Context context) {
        super(context);
        getEditText().setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED);
    }

    @Override public void setText(String text) {
        final boolean wasBlocking = shouldDisableDependents();
        mInteger = parseInteger(text);
        persistString(mInteger != null ? mInteger.toString() : null);
        final boolean isBlocking = shouldDisableDependents(); 
        if (isBlocking != wasBlocking) notifyDependencyChange(isBlocking);
    }

    @Override public String getText() {
        return mInteger != null ? mInteger.toString() : null;
    }

    private static Integer parseInteger(String text) {
        try { return Integer.parseInt(text); }
        catch (NumberFormatException e) { return null; }
    }    
}

The reason why this solves the "0000" issue, is that we're simply casting the user typed value to an int before we store it, and plug it back into a string upon retrieval. That means any leading zeroes (except for '0' as a number of course) will automagically disappear.

The 'number' input type will restrict the user to input only enter numbers, so the parseInteger(...) method is mainly present for sanity reasons, although it will catch a NumberFormatException if you try to enter an empty or null string. Again, you can make this more pretty, which I haven't done for now.

like image 27
MH. Avatar answered Nov 02 '22 06:11

MH.


i guess u can call getEditText() to get a EditText obj , then call setKeyListener with a NumberKeyListener.

like image 42
Grey Avatar answered Nov 02 '22 06:11

Grey


You should use PreferenceActivity which is basically used to show preferences like an xml file. In this activity you can use checkboxes, edit text and save values to preferences.

like image 20
mahima Avatar answered Nov 02 '22 05:11

mahima