1

In my xml I set visiblity condition for control as follows:

android:visibility="@{event.isMessage?(event.dateEventText!=null? View.VISIBLE:View.GONE):View.VISIBLE}"

So, if event.isMessage is true, this: (event.dateEventText!=null? View.VISIBLE:View.GONE) should be evalueated, otherwise, View.VISIBE should be returned.

But data binding throws error message:

****/ data binding error ****msg:Cannot find the setter for attribute 'android:visibility' with parameter type boolean on android.widget.TextView

Does anybody know what's wrong?

user1209216
  • 7,404
  • 12
  • 60
  • 123
  • I tried your code and it works with no compiler errors, try cleaning the project, invalidate cash and restart the AS. – Rainmaker Jan 25 '19 at 22:26

3 Answers3

1

Try this

.
.
.
android:visibility="@{event.isMessage && event.dateEventText!=null ?  View.VISIBLE : View.GONE}"
.
.
.
rgaraisayev
  • 398
  • 3
  • 13
1

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}" />
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
0

You can have boolean to int conversion adapter. If it's static (the same way as BindingAdapter), it will convert boolean fields which expect integer (e.g. View.VISIBLE).

@BindingConversion
int convertBooleanToVisibility(boolean isVisible) {
    return isVisible ? View.VISIBLE : View.GONE;
}

In XML you'd use your method which return boolean for visibiliy:

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="@{event.isMessageVisible()}" />
mlykotom
  • 4,850
  • 1
  • 22
  • 27
  • Why should I use it instead of getter returning `int`? I guess benfit is that it will convert all occurences automatically? Just like converters in wpf binding – user1209216 Jan 25 '19 at 22:16
  • yes, exactly .. the benefit is, that you can use everywhere just boolean flag to show/hide (visible/gone) view – mlykotom Jan 25 '19 at 23:27