Devin Dixon
Devin Dixon

Reputation: 12443

android:visibility Not working with visibile or gone options

I am trying to show and hide an element based on a boolean. In my xml I have the following:

android:visibility="@{viewModel.interfaceEnable ? visible : gone}"

viewModel.interfaceEnable is an ObservableField as as such: var interfaceEnable = ObservableField<Boolean>(). And visible and gone are values for the android:visibility attribute. But I am getting this error:

****/ data binding error ****msg:Identifiers must have user defined types from the XML file. visibile is missing it

Why is this attribute not settable this method?

Upvotes: 0

Views: 467

Answers (2)

Skizo-ozᴉʞS ツ
Skizo-ozᴉʞS ツ

Reputation: 20656

You should use View as follows to use the constants:

android:visibility="@{viewModel.interfaceEnable ? View.VISIBLE : View.GONE}"

For more information check the Visibility documentation that you can use View.GONE, View.INVISIBLE and View.VISIBLE

Also make sure you use the correct import type for this as follows

<data>
    <import type="android.view.View" />
    <variable
        name="anyName"
        type="com.example.AnyName"/>
</data>

Upvotes: 3

Blackbelt
Blackbelt

Reputation: 157487

visible and gone are still constants in View (View.VISIBLE and View.GONE) and that statement should reflect it

android:visibility="@{viewModel.interfaceEnable ? View.VISIBLE : View.GONE}"

alternatively you cold use a simple binding adapter for it. EG

 @BindingAdapter("toVisibility")
 fun View.toVisibility(visible: Boolean) {
    visibility = if (visible) { View.VISIBLE } else { View.GONE }
 }

and in your xml use

toVisibility="@{viewModel.interfaceEnable}"

Upvotes: 1

Related Questions