j2emanue j2emanue - 1 year ago 70
Android Question

Android dataBinding - how to use bool resource to trigger visibility of layout

I currently have a bool.xml file in android which looks like this:

<?xml version="1.0" encoding="utf-8"?>
<bool name="showAds">true</bool>

Now i have a layout.xml file which uses databinding. I want to show or hide the visilibity of a adView based on a the boolean showAds defined above. So far i have this:

android:visibility="@{@bool/showAds ? View.Visible:View:gone}"

but it does not compile. how can i get the boolean to decide if the ad should show or not ?The syntax is wrong.

Answer Source

Correct the syntax of condition View.Visible:View:gone

android:visibility="@{@bool/showAds ? View.VISIBLE:View.GONE}"

and you need to import View in the data section:

    <import type="android.view.View"/>