inputFilter for TextInputEditText

1.3k Views Asked by At

In my application I have an EditText.

This EditText is for currency input, for example: 2.23, 9.99

In other words user can type 2 then . then 23 to this EditText

To achieve this behavior of EditText I implemented an InputFilter like in Mussa's answer of this question EditText with Currency format. And it worked like a charm.

But then I wanted to replace my EditText by construction like this:

    <android.support.design.widget.TextInputLayout
        android:id="@+id/secondary_currency"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/main_currency"
        tools:hint="Болгарский перец">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/secondary_currency_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:textSize="12sp"
            tools:text="348929.00" />
    </android.support.design.widget.TextInputLayout>

Now, I apply my InputFilter to the TextInputEditText.

I was surprised when I didn't manage to type . after 2.

Why I can't to type dot in case of TextInputEditText? How to achieve it?

3

There are 3 best solutions below

2
On BEST ANSWER

With the current layout code. I can see that you have used inputType as number and your desirable has to be decimal.

You can try with inputType as numberDecimal.

Hope it helps.

0
On

I suppose you cannot setFilter to TextInputEditText as it didn't inherit this method from EditText class. I didn't manage t find any similar method in the docs for TextInputEditText But you don't have to use specifically ths class with TextInputLayout. You can replace TextInputEditText with your normal EditText with the filter you had and it'll work as before.

0
On

You can add InputFilter to TextInputEditText.

Kotlin

secondary_currency_input.editableText.filters[0] = yourInputFilter