Reputation: 14863
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
My first problem is the Error Message "Attribute is missing the Android namespace", but all examples i can find don't provide the namespace
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
Upvotes: 1
Views: 919
Reputation: 8106
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.
Upvotes: 0
Reputation: 35539
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>
Upvotes: 0
Reputation: 6834
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>
Upvotes: 1