1

I try to dynamically hide/show an element in my views, therefore i followed this example Dynamically toggle visibility of layout elements with Android data-binding.

I use

  • Android Studio 2.3.1
  • compileSdkVersion 23
  • buildToolsVersion '25.0.2'
  • minSdkVersion 15
  • targetSdkVersion 17

My first problem is the Error Message "Attribute is missing the Android namespace", but all examples i can find don't provide the namespace

enter image description here

nevertheless i tried to start my project and get another error:

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

Error:(22, 29) No resource found that matches the given name (at 'visibility' with value '@{@bool/list_show_icon ? View.VISIBLE : View.GONE}').

It seems that he doesn't try to evaluate the expression

Community
  • 1
  • 1
wutzebaer
  • 14,365
  • 19
  • 99
  • 170

3 Answers3

1

Add your root LinearLayout inside layout tag

<layout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 xmlns:app="http://schemas.android.com/apk/res-auto">

  <data>
     <import type="android.view.View"/>
  </data>
  <LinearLayout>
  </LinearLayout>
</layout>
Kishore Jethava
  • 6,666
  • 5
  • 35
  • 51
0

First rule of using DataBinding is that your root element of XML must be <layout>

is a part of <layout>, not other layouts. In your case it should be

<layout>
    <data> </data>
    <LinearLayout>

    </LinearLayout>
</layout>
Ravi
  • 34,851
  • 21
  • 122
  • 183
0

As Kishore wrote it already.

must be the root element and wrap your whole layout + data.

Its

<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto">
  <data>
      <import type="android.view.View" />
  </data>
 <LinearLayout  
       android:layout_width="match_parent" 
       android:layout_height="match_parent"
       android:background="?android:attr/activatedBackgroundIndicator"
       android:minHeight="56dp"
       android:orientation="horizontal"
       android:paddding="8dp">
  </LinearLayout>
</layout>

Anyway, if you use databinding i recommend using an observable Boolean/Integer instead of using the Visibility logic within the layout. This can be solved using a ViewModel like

 <data>
     <variable name="viewModel" type="YourViewModelClass" />
 </data>

 <LinearLayout> ... <TextView android:visibility="@{viewModel.isVisible}" />
 </LinearLayout>

and using in your ViewModel (mvvm):

 private boolean ObservableInt isVisible = new ObservableInt(View.GONE);
 private void changeVisibility(boolean visible) { isVisible.set( visible ? View.VISIBLE : View.Gone); }

Its just cleaner but doesnt affect the performance or anything.

Emanuel
  • 8,027
  • 2
  • 37
  • 56