Rohaan
Rohaan

Reputation: 41

Setting a minimum and maximum value for android edit text preference

I was wondering if there is a way to enforce a minimum value for the edit text preference.

Here's my node for the EditTextPreference in the preference screen xml file:

    <EditTextPreference
    android:id="@+id/txt_slideshow_speed"
    android:key="@string/prefSlideShowSpeedKey"
    android:persistent="true"
    android:inputType="number"
    android:max="3600"
    android:dialogTitle="Slideshow Speed (seconds)"
    android:defaultValue="@string/defaultSlideShowSpeedValue"
    android:title="Slideshow Speed" />

Upvotes: 4

Views: 13233

Answers (3)

Ravi Parmar
Ravi Parmar

Reputation: 1018

First you need to make a InputFilter class which will restrict the user to enter the values that are not in range.

Public class InputFilterText implements InputFilter {

    private int min, max;

    InputFilterText (int min, int max) {
        this.min = min;
        this.max = max;
    }

    public InputFilterText (String min, String max) {
        this.min = Integer.parseInt(min);
        this.max = Integer.parseInt(max);
    }

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        try {
            int input = Integer.parseInt(dest.toString() + source.toString());
            if (isInRange(min, max, input))
                return null;
        } catch (NumberFormatException nfe) {
            nfe.printStackTrace();
        }
        return "";
    }

    private boolean isInRange(int a, int b, int c) {
        return b > a ? c >= a && c <= b : c >= b && c <= a;
    }
}

Then in onCreate() in PreferenceFragment use this

EditTextPreference editTextPreference = (EditTextPreference) this.findPreference(getString(R.string.EditTextNumberKey));
editTextPreference.getEditText().setFilters(new InputFilter[]{new InputFilterText ("1", "1000")});

Upvotes: 1

user3157423
user3157423

Reputation: 51

You may look at the full documentation of EditText here.

I wasn't able to find a property that describes the minimum value.

If you must, you can do it programmatically by assigning an onTextChange Listener and validation the input.

 EditText text = (EditText)findViewById(R.id.medittext);
    text.addTextChangedListener(new TextWatcher() {
    @Override
    public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            //Validate input to make sure that it fits your requirements




        } 

    });  

Upvotes: 0

Mike Ortiz
Mike Ortiz

Reputation: 4041

First, you will need to get the EditText programmatically via getEditText(). Then, check out this link to set an input filter on your EditText.

Upvotes: 2

Related Questions