Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 359
Thalatta Avatar asked Sep 22 '17 00:09

Thalatta


People also ask

What is the use of databinding in Android?

Data Binding Library Part of Android Jetpack. Stay organized with collections Save and categorize content based on your preferences. The Data Binding Library is a support library that allows you to bind UI components in your layouts to data sources in your app using a declarative format rather than programmatically.

Is databinding better than findViewById method?

Databinding is really faster compared to findViewById and setText . Not only performance, It is also much faster and maintainable for mid, full-scale projects. As a side note, benefits include: performance.

What is 2 way data binding in Android?

Two-way data binding is nothing but updating the data source if there are any changes in the layout and vice versa. Two-way data binding is not applicable for all the views in Android. For example, using two-way data binding in EditText makes sense because users can update the data in the view.


1 Answers

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 }'
    />
like image 164
Linh Avatar answered Sep 30 '22 18:09

Linh