Android support EditTextPreference input type

ivkil picture ivkil · Jan 20, 2016 · Viewed 9.9k times · Source

Is there any way to specify the input method type for android.support.v7.preference.EditTextPreference?

Answer

chevreto picture chevreto · Jan 2, 2019

If you don't want to use a third party library, it is possible to specify a layout to the EditTextPreference

<EditTextPreference android:defaultValue="0"
                        android:key="some_key"
                        android:title="Title"
                        android:dialogLayout="@layout/preference_edit_text"/>

Then in res/layout/preference_edit_text.xml

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText android:id="@android:id/edit"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:inputType="number"
          android:singleLine="true" 
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintBottom_toBottomOf="parent"
          android:layout_marginStart="21dp" 
          android:layout_marginEnd="21dp"/>

</android.support.constraint.ConstraintLayout>

Please note that the edit text id must be : @android:id/edit but then you are free to use whatever you want inside the android:inputType field

I'm sure there's a better way to align the EditText rather than using 21dp margins but at least it works