I'm building a complex layout and I want to use include
tag for my custom component, like this:
<include layout="@layout/topbar"/>
The topbar
layout has custom root component and in layout xml file it's defined like this:
<my.package.TopBarLayout ... a lot of code
Now, I wanna pass my custom defined attributes to "topbar" like this:
<include layout="@layout/topbar" txt:trName="@string/contacts"/>
And then get the value of those custom attributes in custom component code or ideally in xml.
Sadly, I cannot get value of txt:trName
attribute to make it to the topbar
layout, I just don't receive anything in code. If I understand correctly from that documentation page, I can set no attributes for layouts used via include
, but id
, height
and width
.
So my question is how can I pass my custom defined attributes to layout which is added via include
?
Attributes of Layout in Androidandroid:id: It uniquely identifies the Android Layout. android:hint: It shows the hint of what to fill inside the EditText. android:layout_height: It sets the height of the layout. android:layout_width: It sets the width of the layout.
To efficiently re-use complete layouts, you can use the <include/> and <merge/> tags to embed another layout inside the current layout. Reusing layouts is particularly powerful as it allows you to create reusable complex layouts.
I know this is an old question but I came across it and found that it is now possible thanks to Data Binding.
First you need to enable Data Binding in your project. Use DataBindingUtil.inflate
(instead of setContentView
, if it's Activity
) to make it work.
Then add data binding to the layout you want to include:
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android"> <data> <variable name="title" type="java.lang.String"/> </data> <RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/screen_header" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="top" android:gravity="center"> ... <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textSize="20sp" android:textStyle="bold" android:text="@{title}"/> ... </RelativeLayout> </layout>
Finally, pass the variable from the main layout to the included layout like this:
<?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> ... </data> ... <include layout="@layout/included_layout" android:id="@+id/title" app:title="@{@string/title}"/> ... </layout>
It's not possible to attributes other than layout params, visibility or ID on an include tag. This includes custom attributes.
You can verify this by looking at the source of the LayoutInflater.parseInclude method, around line 705: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/view/LayoutInflater.java#640
The inflater only applies the ID and visibility attributes to the included layout.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With