I checked this approach because it looks okay. and it works. However you can check getter setter of Model, View class import in XML.
Following Code Works Well.
Event.class
public class Event {
boolean isMessage;
String dateEventText;
public boolean isMessage() {
return isMessage;
}
public void setMessage(boolean message) {
isMessage = message;
}
public String getDateEventText() {
return dateEventText;
}
public void setDateEventText(String dateEventText) {
this.dateEventText = dateEventText;
}
}
layout_text.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<import type="android.view.View" />
<variable
name="event"
type="com.innovanathinklabs.sample.data.Event" />
</data>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="@{event.isMessage?(event.dateEventText!=null? View.VISIBLE:View.GONE):View.VISIBLE}" />
</layout>
Suggestion:
Move your logical part in Handler.
1. Create Handler
EventHandler.class
public class EventHandler {
private Event event;
public EventHandler(Event event) {
this.event = event;
}
public int getTextVisibility() {
if (event.isMessage && event.dateEventText != null) return View.VISIBLE;
else return View.GONE;
}
}
2. Import Handler in Layout
<variable
name="handler"
type="com.innovanathinklabs.sample.data.EventHandler" />
3. Set handler value from activity
activity.setHandler(new EventHandler(yourEventModel))
4. Use handler method to set visibility
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="@{handler.textVisibility}" />
That's all!
Another Approach
If you don't want add new class of Handler. You can also place visibility method in model class.
1. Put getTextVisibility method in Model
public class Event{
// other variables
public int getTextVisibility() {
if (event.isMessage && event.dateEventText != null) return View.VISIBLE;
else return View.GONE;
}
}
2. Use in layout
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="@{event.textVisibility}" />