0

I could successfully bind UI elements of main.xml to my custom model class. However when i want to use binding with nested toolbar.xml layout, it is not working.

Below are my code snippet. I am getting different errors in different trials.

mainView.xml

<?xml version="1.0" encoding="utf-8"?>
<layout>

    <data>

        <variable
            name="binding"
            type="com.abc.xyz.android.model.NavigationBinding"/>
    </data>

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/parent_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <!-- tool bar to display back image and title -->
        <include
            android:id="@+id/tool_bar_view"
            layout="@layout/tool_bar"
            bind:binding="@{binding}"/> <!-- What to write here -->

        <LinearLayout
            android:id="@+id/ll_bottom_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:orientation="vertical">
          ......
        </LinerarLayout>
        </RelativeLayout>
        </layout>

too_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <variable
            name="binding"
            type="com.abc.xyz.android.model.NavigationBinding"/>
    </data>

    <RelativeLayout
        android:id="@+id/root_toolbar_view"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize">

                <Button
                    android:id="@+id/btn_next1"
                    style="@style/Button.Borderless"
                    android:layout_width="80dp"
                    android:layout_height="match_parent"
                    android:layout_alignParentRight="true"
                    android:enabled="@{person.nextEnabled}"
                    android:text="@string/text_next"/>
....
</RelativeLayout>
</layout>

NavigationBinding.java

public class NavigationBinding extends BaseObservable {

    private boolean isPreviousEnabled = false;
    private boolean isNextEnabled = false;

    @Bindable
    public boolean isPreviousEnabled() {
        return isPreviousEnabled;
    }

    public void setPreviousEnabled(boolean enabled) {
        isPreviousEnabled = enabled;
        notifyPropertyChanged(BR.previousEnabled);
    }

    @Bindable
    public boolean isNextEnabled() {
        return isNextEnabled;
    }

    public void setNextEnabled(boolean enabled) {
        isNextEnabled = enabled;
        notifyPropertyChanged(BR.nextEnabled);
    }
    }

MainActivity.java

 public class MainActivity extends AppCompatActivity{

    private NavigationBinding navigationBinding = null;
    private MainViewBinding uiElementBinding = null;

    @Override
        public void onCreate() {
    navigationBinding = new NavigationBinding();
            uiElementBinding = DataBindingUtil.bind(parentView); // parentView = RecyclerView with parent_view id.
            uiElementBinding.setBinding(navigationBinding);
    }
    }

Can you please help me initialize variables in .java file and how to use them?

pratik03
  • 651
  • 2
  • 7
  • 23
  • https://stackoverflow.com/q/39867165/8089770 https://stackoverflow.com/q/43166731/8089770 – Vidhi Dave Oct 30 '18 at 10:30
  • Possible duplicate of [Include tag and dataBinding](https://stackoverflow.com/questions/39867165/include-tag-and-databinding) – Vidhi Dave Oct 30 '18 at 10:30
  • But how to declare and use in .java file? In above both links it is not mentioned. – pratik03 Oct 30 '18 at 11:02
  • try https://stackoverflow.com/a/51638633/8089770 or search in google for data binding use included layout in java android – Vidhi Dave Oct 30 '18 at 11:07
  • @VishvaDave: I got your links and it very helpful. I need some more help. I have implemented a custom class which decides button should be enabled or not. I dn't want to use directly like binding.includedLayout.textView.setText("text"); – pratik03 Oct 30 '18 at 11:30
  • may be you can have interface type things in which method can have button as parameter. not sure for this actually but can try this or just edit question so other people can also help – Vidhi Dave Oct 30 '18 at 11:42
  • Your question is unclear, where are you stuck? `initialize variables in .java` - can you tell more about this. – Khemraj Sharma Oct 31 '18 at 05:34

0 Answers0