0

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?

Devin Dixon
  • 11,553
  • 24
  • 86
  • 167
  • Can you post full xml please? – Skizo-ozᴉʞS ツ Apr 09 '19 at 15:01
  • Possible duplicate of [Android Data binding - Error:(119, 29) Identifiers must have user defined types from the XML file. main\_radio\_subscribe is missing it](https://stackoverflow.com/questions/41609252/android-data-binding-error119-29-identifiers-must-have-user-defined-types) – Martin Zeitler Apr 09 '19 at 15:12

2 Answers2

3

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>
Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
1

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}"
Blackbelt
  • 156,034
  • 29
  • 297
  • 305