Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Databinding expression with enum comparison

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?

like image 376
jennymo Avatar asked May 15 '17 09:05

jennymo


People also ask

Which is better ViewBinding and Data Binding?

The main advantages of viewbinding are speed and efficiency. It has a shorter build time because it avoids the overhead and performance issues associated with DataBinding due to annotation processors affecting DataBinding's build time.

When should I use Data Binding?

Data Binding allows you to effortlessly communicate across views and data sources. This pattern is important for many Android designs, including model view ViewModel (MVVM), which is currently one of the most common Android architecture patterns.

What is 2 way Data Binding in Android?

Stay organized with collections Save and categorize content based on your preferences. The @={} notation, which importantly includes the "=" sign, receives data changes to the property and listen to user updates at the same time.

Which is the correct way to reference bound data in the XML layout?

Layout Binding expressions Expressions in the XML layout files are assigned to a value of the attribute properties using the “ @{} " syntax. We just need to use the basic syntax @{} in an assignment expression.


1 Answers

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> 
like image 158
Shohn Avatar answered Sep 28 '22 10:09

Shohn