0

I'm trying to create a reusable layout included.xml which then could be injected into other layouts and customized via tag attributes. Here's what I have:

res/layout/parent.xml:

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

    <include layout="@layout/included"
            app:src123="@drawable/my_icon" />

</layout>

res/layout/included.xml:

<?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"
    >
    <data>
        <variable name="src123" type="android.graphics.drawable.Drawable" />
    </data>

    <android.support.design.widget.FloatingActionButton
        android:src="@{src123}" />
</layout>

app/build.gradle:

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    dataBinding {
        enabled = true
    }
    ....
}

dependencies {
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
}

As an outcome, the button which I try to inject doesn't contain any image at all.

If in parent.xml I change xmlns:app to res-auto, I have the following error in app/build/intermediate/data-binding-layout-out/debug/layout/parent.xml:

Error:(17) No resource identifier found for attribute 'src123' in package 'com.myself.fancyapp'

Does anybody have an idea why this happens and how to fix this? Thanks.

6
  • xmlns:app="http://schemas.android.com/apk/res-auto" keep this in parent.xml and see the output Commented Dec 15, 2016 at 4:45
  • @RaviRupareliya Thanks for the suggestion but, as mentioned in the last part of my post, it throws a compile time error. Commented Dec 15, 2016 at 7:18
  • for a confirmation are you implementing DataBinding in a class where you are using parent.xml? or you are simply using setContentView or inflater for parent.xml? Commented Dec 15, 2016 at 7:20
  • @RaviRupareliya I just use setContentView(R.layout.parent), I don't implement DataBinding in class. Commented Dec 15, 2016 at 7:38
  • Than you must have to use DataBinding in that class also Commented Dec 15, 2016 at 7:40

1 Answer 1

3

The problem is that you're not using binding syntax for the variable:

<include layout="@layout/included"
        app:src123="@drawable/my_icon" />

should be:

<include layout="@layout/included"
        app:src123="@{@drawable/my_icon}" />

Unrelated, but I don't think that includes are allowed as the root element of a layout. I believe that you're going to have to have a normal View surrounding the include.

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

    <FrameLayout android:layout_width="match_parent"
                 android:layout_height="match_parent">
        <include layout="@layout/included"
                app:src123="@{@drawable/my_icon}" />
    </FrameLayout>
</layout>
Sign up to request clarification or add additional context in comments.

Comments

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.