data binding – Android: Databinding expression with enum comparison-ThrowExceptions

Exception or error:

is it possible to create a databinding expression and control the visibility of a view element by using enumerations? What I want to achieve is the following

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:visibility="@{user.editType == EditType.EDIT_PROFIL ? View.VISIBLE : View.GONE}">

The EditType class is very simple

public enum EditType {
  NONE,
  EDIT_PROFIL,
  EDIT_ADDRESSES; }

It would be awesome if I can use this enumeration within the XML to control the visibility of my LinearLayout.

Anybody got an idea how to achieve this?

How to solve:

To me it seems like what you want to achieve is possible and I dont see anything wrong except maybe u lack of the tag? I dont see it…

<data>
 <import type="com.example.my.app.EditType"/>
</data>

###

I would recommed you to separate the logic from the UI. All logic of the visibility should be done in java code in diferent events. I would say something like @Demo_Mail suggested would work if implemented on the OnCreate() method of your Activity/Fragment but reading the data-binding tag suggests me that you want to do this dynamically, at any point in the run (correct me if Im wrong). In that case I would suggest to create an OnClick() or OnSelectItem() method that will be executed in the moment the user selects a value of the enum (in a comboBox for example). This method will set the visibility of the view.

Leave a Reply

Your email address will not be published. Required fields are marked *