Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditTextPreference - only numeric value inputType - isn't working

<android.support.v7.preference.EditTextPreference
            android:key="prefTest"
            android:title="test number input"
            android:inputType="numberDecimal|numberSigned"
            android:defaultValue="800"/>

It still shows regular keyboard and allows me to type any character

Is there something wrong with android.support.v7?

like image 493
user25 Avatar asked Mar 15 '17 08:03

user25


4 Answers

For androidx library i used

    val preference = findPreference("pref_key") as EditTextPreference?
    
    preference!!.setOnBindEditTextListener { 
            editText -> editText.inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_SIGNED
    }

inside onCreatePreferences function for PreferenceFragmentCompat

like image 155
mpountou Avatar answered Sep 28 '22 08:09

mpountou


The Customize your settings section in the developer guide for settings recommends that you set the input type programmatically by using an OnBindEditTextListener as follows:

public class SettingsFragment extends PreferenceFragmentCompat {
    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.settings_screen, rootKey);

        EditTextPreference weeklyGoalPref = findPreference("weekly_goal");
        if (weeklyGoalPref != null) {
            weeklyGoalPref.setOnBindEditTextListener(new EditTextPreference.OnBindEditTextListener() {
                @Override
                public void onBindEditText(@NonNull EditText editText) {
                    editText.setInputType(InputType.TYPE_CLASS_NUMBER);
                }
            });
        }
    }
}

I tried defining the input type as "number" in xml and still got the normal keyboard with letters. This approach worked for me.

like image 41
schv09 Avatar answered Nov 04 '22 08:11

schv09


android:digits="0123456789"

use this in edittext as it accepts only defined numbers. if its not working then use Android-Support-Preference-V7-Fix library.

Fixed EditTextPreference forwards the XML attributes (like inputType) to the EditText, just like the original preference did.

like image 8
Shivanshu Verma Avatar answered Nov 04 '22 07:11

Shivanshu Verma


I agree that original support preferences has some issues, but for resolving this issue we just need add custom layout and specify EditText with android:inputType="number"

<android.support.v7.preference.EditTextPreference
            android:dialogLayout="@layout/preference_dialog_edittext_custom"

So that you may copy original preference_dialog_layout.xml file and edit it.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="48dp"
    android:layout_marginBottom="48dp"
    android:overScrollMode="ifContentScrolls">

  <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginStart="24dp"
      android:layout_marginEnd="24dp"
      android:orientation="vertical">

    <TextView android:id="@android:id/message"
        style="?android:attr/textAppearanceSmall"
        android:layout_marginBottom="48dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="?android:attr/textColorSecondary" />

    <EditText
        android:id="@android:id/edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:layout_marginStart="-4dp"
        android:layout_marginEnd="-4dp" />

  </LinearLayout>

</ScrollView>
like image 2
brucemax Avatar answered Nov 04 '22 08:11

brucemax