55

how to set only numeric value for EditTextPreference in android. I want a user to enter a port number not sure how can I put the limitation there

I am using this code, user can enter any string. Want to limit the user to atleast numbers only

   <EditTextPreference 
    android:defaultValue="4444" 
    android:key="port" 
    android:title="Port" 
    android:dependency="service_on"        
    />    
Ahmed
  • 14,503
  • 22
  • 92
  • 150

7 Answers7

88

EditTextPreference widgets should take the same attributes as a regular EditText, so use:

android:inputType="number"

Or more specifically use:

android:inputType="numberDecimal"
android:digits="0123456789"

since you want to limit the input to a port number only.

Sam
  • 86,580
  • 20
  • 181
  • 179
  • thanks, EditTextPreference is not a decendent of EditText so I just wonder where is this mentioned in the android documentation ? Why do things sometime work magically in android without having any docs ? can you please share a reference ? thanks – Ahmed Nov 16 '12 at 22:31
  • 18
    "Why do things sometime work magically in android without having any docs ?" Yes, this one is _poorly_ documented, the _hint_ is [here](http://developer.android.com/reference/android/preference/EditTextPreference.html). The line "See EditText Attributes" is the only hint... Anyway you might want `"numberDecimal"` or consider using the [digits](http://developer.android.com/reference/android/widget/TextView.html#attr_android%3adigits) attribute with android:digits="0123456789.:" to use numbers, periods, and colons only. – Sam Nov 16 '12 at 22:34
  • 40
    This does not appear to work appcompat 25, shows regular keyboard – JohnC Nov 16 '16 at 18:50
  • Note that the value is still stored in the preferences as a string, so you'll need to parse it when you retrieve it (if you want an integer to use). – Geoff Jun 06 '17 at 16:04
  • 4
    I can't get this to work. Nothing blocks input from being literally anything, and saving as literally anything :( – Elliptica Jul 18 '18 at 18:43
  • 4
    Does not even work in _androidx.preference.PreferenceFragmentCompat_. Star this [issue](https://issuetracker.google.com/issues/37060038) - but don't live in hope. Either rewrite code that **once worked** as per other answers OR use this [well-respected fix](https://github.com/Gericop/Android-Support-Preference-V7-Fix) that has been recently updated for _implementation "androidx.preference:preference:1.1.0"_. – Bad Loser Dec 14 '19 at 08:17
42

I use an androidX library because this library has a possibility to customize an input type of EditTextPreference dialog. AndroidX is a major improvement to the original Android Support Library so is suggested that everybody use this library. You can read more about AndroidX here.

Here is my code where I use EditTextPreference inside of onCreatePreference method:

 @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.preference, rootKey);



androidx.preference.EditTextPreference editTextPreference = getPreferenceManager().findPreference("use_key_from_editTextPreference_in_xml_file");
editTextPreference.setOnBindEditTextListener(new androidx.preference.EditTextPreference.OnBindEditTextListener() {
            @Override
            public void onBindEditText(@NonNull EditText editText) {
                editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED);

            }
        });

       }

After you use this code and click on editTextPreference, the dialog will pop up and your keyboard input type will be only numeric.

Dezo
  • 835
  • 9
  • 16
  • 11
    kinda annoys me that you can't get it to work with just the XML – J-Cake Oct 09 '19 at 22:38
  • @JacobSchneider yup, just makes me want to leave it like it is. – lasec0203 Aug 26 '20 at 04:36
  • 1
    It works, but I had to remove the @NonNull annotation. Also it displays special characters like %&/* which do not enter anything. Hint: if you remove the "InputType.TYPE_NUMBER_FLAG_SIGNED" portion it will not let you enter the minus sign, which is what I needed. – Marcell Dec 22 '20 at 18:46
21

Using androidx.preference library, since androidx.preference:preference:1.1.0-alpha02 which is realeased on 2018.12.17, the library adds EditTextPreference.OnBindEditTextListener interface, which allows you to customize the EditText displayed in the corresponding dialog after the dialog has been bound.

So in this case, all you have to do is to add the Kotlin code below.

val editTextPreference = preferenceManager.findPreference<EditTextPreference>("YOUR_PREFERENCE_KEY")
editTextPreference.setOnBindEditTextListener { editText ->
                editText.inputType = InputType.TYPE_CLASS_NUMBER
}
kostyabakay
  • 1,649
  • 2
  • 21
  • 32
Brainor
  • 350
  • 2
  • 10
  • 2
    Unfortunately, this callback is not working for Android-TV's LeanbackEditTextPreferenceDialogFragmentCompat. A workaround can be found here: https://stackoverflow.com/a/56225993/2806862 – Itay Bianco May 20 '19 at 18:03
  • @ItayBianco yes you are right. This particular interface hasn't implemented in the `Leanback-Preference` package yet. The method I mentioned is only available in `EditTextPreference` as far as I know. – Brainor May 21 '19 at 05:11
9

EditTextPreference with decimal number input (Kotlin):

var myEditTextPreference : EditTextPreference? = findPreference("myEditTextPreferenceKey")

    myEditTextPreference?.setOnBindEditTextListener { editText ->
        editText.inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL

    }
user13407337
  • 91
  • 1
  • 2
4

Kotlin Code which worked for me:

Here "editTextPrefSleepTime" is a key of EditTextPreference, which I set on xml file.

    private var editTextPrefSleepTime: EditTextPreference? = null

     override fun onStart() {
         super.onStart()
         editTextPrefSleepTime = preferenceManager.findPreference("editTextPrefSleepTime")
         editTextPrefSleepTime?.setOnBindEditTextListener {
             it.inputType = InputType.TYPE_CLASS_NUMBER
         }
     }
Touhid
  • 1,556
  • 18
  • 18
3

Access the EditTextPreference's EditText methods with getEditText and set the input type

EditTextPreference pref = (EditTextPreference) findPreference("pref_edit_text");
if (pref != null) {
    pref.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER);
}

Unfortunate the offical library android.support.v7.preference hide access to getEditText, but there is a fix library that soloves this.

Selti99
  • 31
  • 1
0

For me, I wanted to allow digits and dots only so that users can type an IP address.

This is JAVA

  androidx.preference.EditTextPreference toesIpText = getPreferenceManager()
    .findPreference("toes_ip_address");
  assert toesIpText != null;
  toesIpText.setOnBindEditTextListener(editText -> {
    editText.setInputType(InputType.TYPE_CLASS_TEXT);
    editText.setKeyListener(DigitsKeyListener.getInstance("123456789."));
  });