1

Hi I have made layout with couple of ImageView and TextView elements due to reduction of code duplication. I have occurred to two problems:

  1. I need to update these elements inside of layout using data binding, but I don't know how can I access to them in order to update images and texts?
  2. The visibility of layout should be bind from VM object(default visibility is GONE) but it doesn't work. Even if I update VM setter for visibility, the layout is till visible on the screen.

The code:

included layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/details"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="#FF0000">

        <ImageView
            android:id="@+id/image_details"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/text_details"
            android:text="details"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/favorites"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="#00FF00">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:text="details"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/record"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="#0000FF">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:text="details"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/remind_me"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="#FF0000">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:text="details"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/return_to_begin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="#00FF00">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:text="details"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>


</LinearLayout>

activity layout(this is a snippet, the whole xml is too large)

<include layout="@layout/manu_layout"
            android:layout_above="@+id/info"
            android:layout_alignParentRight="true"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:visibility="@{vm.infoMenu}"/>

viewModel method for visibility:

 public void setInfoMenu() {
        if(menuVisibility == View.VISIBLE){
            menuVisibility = View.GONE;
            setMediaControlView(false);
        }else {
            menuVisibility=View.VISIBLE;
            setMediaControlView(true);

        }
        notifyPropertyChanged(BR.infoMenu);
    }
    @Bindable
    public int getInfoMenu(){
        return menuVisibility;
    }

Please let me know if you need any additional code snippet.

Can you please help me to solve these problems?

Thanks,

Hogar

svarog
  • 688
  • 1
  • 12
  • 29

1 Answers1

1

Make sure you have <layout> as a root tag of your xml file if you want to use DataBinding.

You can have a id of your <include> and acceess controls of included layout via it. For example you have a id as 'included' of your <include> then you can access it as binding.included.details or binding.included.favourite

Ravi
  • 34,851
  • 21
  • 122
  • 183
  • I use as root element, that is not the problem. The problem is that I don't know how to access each `textView` or `imageView` of included layout. For example, let's say that we need to update Image and Text for it... which attribute I need to use change these views? Maybe I didn't explain my problem in right way? – svarog Jun 14 '17 at 11:40
  • 1
    yes that is what i am telling you, access it through the ids, like i have explained it in answer `binding.included.details`. Make sure your included layout should also have `` as root tag. Refer [this](https://stackoverflow.com/a/32958608/3134215) answer also. – Ravi Jun 14 '17 at 11:42
  • aha, thanks buddy :) will try this and let you know the outcome – svarog Jun 14 '17 at 11:44