Search code examples
androiddata-binding

How to set android:inputType using databinding


I am trying to toggle between the old way of displaying a password with the following databinding expression:

android:password="@{isMyFlagTrue}"

However when I try to use the inputType in lieu of the now deprecated password attribute, I can't seem to successfully set the password type. I have tried:

android:inputType="@{isMyFlagTrue? InputType.TYPE_TEXT_VARIATION_PASSWORD : InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD)}"

But this made no effect on the inputType of the EditText view. Even when I try to set it directly to the invisible variety it is still visible.

Any suggestions?


Solution

  • You can set input type use data-binding like

    <EditText
        ...
        android:inputType='@{condition ? (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD) : InputType.TYPE_CLASS_TEXT }'
        />
    

    after importing InputType like

    <data>
        ...
        <import type="android.text.InputType" />
    </data>