Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android support EditTextPreference input type

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

like image 307
ivkil Avatar asked Jan 20 '16 17:01

ivkil


People also ask

How to set input type in android studio?

The android:inputType attribute allows you to specify various behaviors for the input method. Most importantly, if your text field is intended for basic text input (such as for a text message), you should enable auto spelling correction with the "textAutoCorrect" value.

How to add text field in android?

Click on the "Plain Text" label, and drag it to your designer. If you click on the button below, you will see the xml code. The textbox is called EditText.

What is textCapSentences?

textCapSentences. Can be combined with text and its variations to request capitalization of the first character of every sentence. Corresponds to TYPE_TEXT_FLAG_CAP_SENTENCES .

What is an InputType?

android.text.InputType. Known indirect subclasses. EditorInfo. EditorInfo. An EditorInfo describes several attributes of a text editing object that an input method is communicating with (typically an EditText), most importantly the type of text content it contains and the current cursor position.


2 Answers

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

like image 116
chevreto Avatar answered Oct 04 '22 22:10

chevreto


Now one can 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 24
ivkil Avatar answered Oct 04 '22 21:10

ivkil