7

I am trying to pass a simple string to an layout from my main layout using the Android data binding feature. It compiles fine, but the value being passed to the include is not actually being passed. i.e. - it does not show up in my layout.

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

    <data>
        <variable name="mytitle" type="java.lang.String"/>
    </data>

    <android.support.constraint.ConstraintLayout
        android:background="@color/black_background"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

 <include
            android:id="@+id/include1"
            layout="@layout/mylayout"
            app:mytitle="@{@string/categories}"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"/>

    </android.support.constraint.ConstraintLayout>
</layout>

My Include Layout is :

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable name="mytitle" type="java.lang.String"/>
    </data>

<android.support.constraint.ConstraintLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="@color/black_background"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/category_header_textview"
        style="@style/WhiteText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@{mytitle}"
        app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>
</layout>

1 Answer 1

8

OK. Figured it out. Posting here for others. The thing I was missing was actually using the DataBindingUtil to set the content view.

Add the following to your onCreate():

DataBindingUtil.setContentView(this, R.layout.mylayout);
Sign up to request clarification or add additional context in comments.

1 Comment

Great :D, obviously data binding will not work with direct setContentView().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.