I wanted to reuse text fields and hold them separately in XML file as independent components. I'm using data binding to bind string resources.
It worked fine if those components were not split into independent XML files, but now as I've used include tag, it is not displaying any resource strings in App. What can be wrong?
Code example:
Main layout
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable name="CustomRes" type="com.project.utils.CustomResources"/>
</data>
<LinearLayout
android:id="@+id/mainContentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="@dimen/padding_medium">
<include layout="@layout/form_name"/>
<include layout="@layout/form_email"/>
<include layout="@layout/form_phone"/>
</LinearLayout>
</layout>
Included Layout example:
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable name="CustomRes" type="com.project.utils.CustomResources"/>
</data>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/nameLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/TextInputLayoutStyle"
app:errorEnabled="true"
android:hint='@{CustomRes.stringValues["form_name"]}'>
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/nameEt"
android:layout_width="match_parent"
android:inputType="textCapWords"
android:singleLine="true"
app:errorEnabled="true"
android:focusable="true"
android:focusableInTouchMode="true"
style="@style/TextInputEditTextStyle"/>
</com.google.android.material.textfield.TextInputLayout>
</layout>
You need to add the ID to the include
tag:
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:id="@+id/mainContentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="@dimen/padding_medium">
<include layout="@layout/form_name"
android:id="@+id/name_layout_include"/>
<include layout="@layout/form_email"/>
<include layout="@layout/form_phone"/>
</LinearLayout>
Included Layout example:
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/TextInputLayoutStyle"
app:errorEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/name_et"
android:layout_width="match_parent"
android:inputType="textCapWords"
android:singleLine="true"
app:errorEnabled="true"
android:focusable="true"
android:focusableInTouchMode="true"
style="@style/TextInputEditTextStyle"/>
</com.google.android.material.textfield.TextInputLayout>
</layout>
Then you can address it by calling:
mBinding.nameLayoutInclude.nameEt
Since Viewbinding is more future proof than Databinding, it's better to just go this way.
Try this
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable name="CustomRes" type="com.project.utils.CustomResources"/>
</data>
<LinearLayout
android:id="@+id/mainContentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="@dimen/padding_medium">
<include
app:CustomResIncluded ="@{CustomRes}"
layout="@layout/form_name"/>
</LinearLayout>
</layout>
and include_layout :
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable name="CustomResIncluded" type="com.project.utils.CustomResources"/>
</data>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/nameLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/TextInputLayoutStyle"
app:errorEnabled="true"
android:hint='@{CustomResIncluded.stringValues["form_name"]}'>
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/nameEt"
android:layout_width="match_parent"
android:inputType="textCapWords"
android:singleLine="true"
app:errorEnabled="true"
android:focusable="true"
android:focusableInTouchMode="true"
style="@style/TextInputEditTextStyle"/>
</com.google.android.material.textfield.TextInputLayout>
</layout>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.