<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
?
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
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With