android binding value to include layout is failed, - android

I try setting value to include layout.
I wrapped the root layout to tag and passed using app:headerTitle.
But I got this error
AAPT: C:\Users\ckdrb\Desktop\EMOPlayer\app\build\intermediates\incremental\mergeDebugResources\stripped.dir\layout\activity_equalizer.xml:11: error: attribute headerTitle (aka com.jakchang.emo:headerTitle) not found.
error: failed linking file resources.
I don't understand what's wrong, Because I followed so many examples.
equalizer.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/player_background_theme"
android:orientation="vertical">
<include
android:id="#+id/appbar"
layout="#layout/layout_part_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:headerTitle="bas123"/>
</LinearLayout>
</layout>
layout_part_appbar.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="headerTitle"
type="String" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/loginHeaderLayout"
android:layout_width="match_parent"
android:layout_height="56dp">
<Button
android:id="#+id/backButton"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginStart="5dp"
android:background="#drawable/back_arrow_black"
app:backgroundTint="#null"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/headerTitleText"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:textAlignment="center"
android:textSize="#dimen/appbar_title_text_size"
android:textStyle="bold"
android:textColor="#color/colorBlack"
android:layout_marginStart="10dp"
android:text="#{headerTitle}"
app:layout_constrainedWidth="true"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/backButton"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

To pass the value using data binding you need to set it as below
app:headerTitle="#{`bas123`}"

In the data section, you need to use fully qualified path or import the class, like in Java.
Using full path:
<data>
<variable
name="headerTitle"
type="java.lang.String" />
</data>
Using import:
<data>
<import type="java.lang.String"/>
<variable
name="headerTitle"
type="String" />
</data>

I have achieved this via code. First remove app:headerTitle="bas123" declared inside equalizer.xml. Then in fragment or activity set data to variable headerTitle. Look below code:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = DataBindingUtil.setContentView<EqualizerBinding>
(this,R.layout.equalizer)
binding.appbar.headerTitle = "My Header"
}

Like app:headerTitle="#{#string/app_name}" in equalizer.xml file

Related

Hilt Data binding: One specific view is getting "Could not find identifier"

I have a working app with Hilt. Added the layout below but this specific spinner view is having problems while the rest is working fine. The spinner data binding itself is working and entries being populated, but for some reason this part cannot find the view id tags_spinner.
<ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
android:onClick="#{(view) -> viewModel.process(tags_spinner)}"/>
The unusual thing is the other image button can find its target view just fine.
`<ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
android:onClick="#{(view) -> viewModel.process(tag)}"/>`
My app is running on these versions:
hilt_version = '2.40.5'
lifecycle_version = '2.4.1'
kapt_version = '1.6.20-RC'
Layout code below.
<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="viewModel"
type="com.base.BaseActivityViewModel" />
<variable
name="adapter"
type="android.widget.ArrayAdapter" />
<import type="android.view.View"/>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/trackerNameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Page Name"
android:text="#={viewModel.page.pageName}"
app:layout_constraintTop_toTopOf="parent"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/trackerNameEditText">
<Spinner
android:id="#+id/tags_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:listitem="#android:layout/simple_spinner_dropdown_item"
android:setAdapter="#{adapter}"
android:entries="#{viewModel.tagsList}"/>
<ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
android:onClick="#{(view) -> viewModel.process(tags_spinner)}"/>
<EditText
android:id="#+id/tag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:hint="New Tag"/>
<ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
android:onClick="#{(view) -> viewModel.process(tag)}"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Found that it is a bug which I haven't fully understood yet, it causes the problem when assigning view ids containing an undescrore _. Simply changing my assignment to "#+id/tagsSpinner" fixed the problem.

Unable to find symbol FragmentBindingImpl with databinding bindingadapter android?

Below is my main layout where I have included another layout
<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="userName"
type="String" />
<variable
name="hasEnded"
type="Boolean" />
<variable
name="showLoader"
type="Boolean" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/parent_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/base_chat_background">
<com.commonui.AppBar
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#{userName}"
app:addWindowInsetPaddingTop="#{true}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:clipToPadding="false"
android:paddingTop="16dp"
android:paddingBottom="70dp"
app:addWindowInsetPaddingBottom="#{true}"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/app_bar" />
<include
android:id="#+id/feedbackLayout"
visibleIf="#{hasEnded}"
layout="#layout/chat_feedback"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<ProgressBar
android:id="#+id/loader"
visibleIf="#{showLoader}"
android:layout_width="44dp"
android:layout_height="44dp"
android:layout_gravity="center"
android:layout_marginTop="16dp"
android:indeterminate="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/app_bar" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
I have an extension function with which I am handling the visibility of views in XML
#BindingAdapter("visibleIf")
fun setVisibleIf(view: View, visible: Boolean) {
view.visibility = if(visible) View.VISIBLE else View.GONE
}
When I add visibleIf="#{hasEnded}" to my included layout and build the project I get the error
error: cannot find symbol
import com.chat.databinding.FragmentPeerchatBindingImpl;
symbol: class FragmentChatBindingImpl
location: package com.chat.databinding
What could be the cause of it?
Apparently included layout doesn't support binding adapter for now
Alternative is to send your variable from main layout to included layout
We can use app:showFeedback="#{hasEnded}" here showFeedback is the binding variable defined in included layout. hasEnded is the variable defined in the main layout

With data binding, How to pass function(or lambda) variable to included layout

I will set editor action to edittext.
My first code is
#BindingAdapter("onEditorAction")
fun bindOnEditorAction(view: TextView, event: () -> Unit) {
view.setOnEditorActionListener { _, _, _ ->
event()
true
}
}
...
<EditText
...
app:onEditorAction="#{vm::searchAction}"
... />
This is working well
but when I use include tag, I don't know how to pass #{vm::searchAction} as variable like this:
activity.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"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="vm"
type="com.example.MyViewModel" />
</data>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:fillViewport="true"
android:overScrollMode="never">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include
android:id="#+id/input"
layout="#layout/view_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:action="#{vm::searchAction}"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:text="#={vm.input}" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</layout>
view_input_layout.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"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="text"
type="String" />
<variable
name="action"
type="???" /> // **What type for event??**
</data>
<com.google.android.material.card.MaterialCardView
android:id="#+id/input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<EditText
android:id="#+id/input_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:text="#{text}
app:editorAction=#{action} /> // Passed action will be setted here!
<TextView
android:id="#+id/timer_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|end"
android:layout_marginEnd="20dp"
tools:text="00:00" />
</com.google.android.material.card.MaterialCardView>
</layout>
MyViewModel:
class MyViewModel(): ViewModel() {
val input = ObservableField<String>()
fun searchAction() {
Log.i("searchAction", "$input")
}
}
Is there any way to solve this problem?
It's an old question, but I answer for others who need help.
Try like this code.
<import type="kotlin.jvm.functions.Function0" />
<import type="kotlin.Unit" />
<variable
name="action"
type="Function0<Unit>" />
...
android:onClick="#{() -> action.invoke()}"
...
If your function has many params, you can try like this.
<import type="kotlin.jvm.functions.Function2" />
<import type="kotlin.Unit" />
<variable
name="action"
type="Function2<Integer, String, Unit>"/>
Try This:
activity.xml:
......
<include
android:id="#+id/includedInput"
layout="#layout/view_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
.....
view_input_layout.xml:
<data>
<variable
name="text"
type="String" />
<variable
name="vm"
type="com.example.MyViewModel" />
</data>
<com.google.android.material.card.MaterialCardView
android:id="#+id/input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<EditText
...
android:text="#{text}"
app:onEditorAction="#{vm::searchAction}"
... />
<TextView
android:id="#+id/timer_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|end"
android:layout_marginEnd="20dp"
tools:text="00:00" />
</com.google.android.material.card.MaterialCardView>
Now inside your activity you can access view_input_layout.xml by includedInput id(from activity.xml)
Initialize viewModel inside your activity something like this
includedInput?.vm = viewModel
inlcudedInput?.text = "hello this is your text"

How to override xml view (that use data Binding) from Library in my project (# Override_Views_Data_Binding_Android )

First of all I have deep search for that and I didn't find any related topic or solution :(
I have created a library project and import it as a library in another project, this library in all of its views use Android dataBinding, I need to override a xml view or portion of it(say include) in my project
as follow :
product_details_quantity_layout.xml in Library
<?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">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/cl_quantity"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/dim_product_details_sc_quantity_view_mrgn_top"
android:layout_marginBottom="#dimen/dim_product_details_sc_quantity_view_mrgn_bottom"
android:background="#drawable/product_quantity_bg"
android:gravity="center"
android:layoutDirection="ltr"
android:paddingStart="#dimen/dim_product_details_sc_quantity_view_padding_start"
android:paddingTop="#dimen/dim_product_details_sc_quantity_view_padding_top"
android:paddingEnd="#dimen/dim_product_details_sc_quantity_view_padding_start"
android:paddingBottom="#dimen/dim_product_details_sc_quantity_view_padding_bottom"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.42">
<ImageView
android:id="#+id/iv_decrement"
multipleClick="#{true}"
android:layout_width="0dp"
android:layout_height="0dp"
android:onClick="#{decreaseBtnClick}"
android:src="#drawable/ic_minus"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1.4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.15" />
<ImageView
android:id="#+id/iv_increment"
multipleClick="#{true}"
android:layout_width="0dp"
android:layout_height="0dp"
android:onClick="#{increaseBtnClick}"
android:src="#drawable/ic_add"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1.4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.15" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#{productQty}"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/iv_increment"
app:layout_constraintStart_toEndOf="#id/iv_decrement"
app:layout_constraintTop_toTopOf="parent"
tools:text="1" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<data>
<import type="android.view.View" />
<variable
name="productQty"
type="String" />
<variable
name="increaseBtnClick"
type="android.view.View.OnClickListener" />
<variable
name="decreaseBtnClick"
type="android.view.View.OnClickListener" />
</data>
</layout>
And in my project
product_details_quantity_layout.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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".ui.view.product_details.ProductDetailsActivity"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/cl_quantity"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layoutDirection="ltr"
android:paddingStart="#dimen/dim_product_details_sc_quantity_view_padding_start"
android:paddingTop="#dimen/dim_product_details_sc_quantity_view_padding_top"
android:paddingEnd="#dimen/dim_product_details_sc_quantity_view_padding_start"
android:paddingBottom="#dimen/dim_product_details_sc_quantity_view_padding_bottom"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.42">
<ImageView
android:id="#+id/iv_decrement"
multipleClick="#{true}"
android:layout_width="0dp"
android:layout_height="0dp"
android:onClick="#{decreaseBtnClick}"
android:src="#drawable/ic_minuse_circle"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.23" />
<ImageView
android:id="#+id/iv_increment"
multipleClick="#{true}"
android:layout_width="0dp"
android:layout_height="0dp"
android:onClick="#{increaseBtnClick}"
android:src="#drawable/ic_add_circle"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.23" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#{productQty}"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/iv_increment"
app:layout_constraintStart_toEndOf="#id/iv_decrement"
app:layout_constraintTop_toTopOf="parent"
tools:text="1" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<data >
<import type="android.view.View" />
<variable
name="productQty"
type="String" />
<variable
name="increaseBtnClick"
type="android.view.View.OnClickListener" />
<variable
name="decreaseBtnClick"
type="android.view.View.OnClickListener" />
</data>
</layout>
In this I got exception class cast exception, it can't cast the generated databinding class from child to the dataBinding in Parent
so I updated the tag in my project xml file to
after that it work fine and inflate the new view, but when generating new APK it give duplicated class error that the found twice in parent and child projects
so dears any help in this case will be highly appreciated.
Update
This my Include tag in the full xml file in parent
<include
android:id="#+id/cl_quantity"
layout="#layout/product_details_quantity_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:decreaseBtnClick="#{()->viewModel.onDecreaseQty()}"
app:increaseBtnClick="#{()->viewModel.onIncreaseQty()}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:productQty="#{String.valueOf(viewModel.productQty)}" />
Second Update Adding Import
<?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"
tools:context=".ui.view.product_details.ProductDetailsActivity"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/cl_quantity"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layoutDirection="ltr"
android:paddingStart="#dimen/dim_product_details_sc_quantity_view_padding_start"
android:paddingTop="#dimen/dim_product_details_sc_quantity_view_padding_top"
android:paddingEnd="#dimen/dim_product_details_sc_quantity_view_padding_start"
android:paddingBottom="#dimen/dim_product_details_sc_quantity_view_padding_bottom"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.42">
<ImageView
android:id="#+id/iv_decrement"
multipleClick="#{true}"
android:layout_width="0dp"
android:layout_height="0dp"
android:onClick="#{decreaseBtnClick}"
android:src="#drawable/ic_minuse_circle"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.23" />
<ImageView
android:id="#+id/iv_increment"
multipleClick="#{true}"
android:layout_width="0dp"
android:layout_height="0dp"
android:onClick="#{increaseBtnClick}"
android:src="#drawable/ic_add_circle"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.23" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#{productQty}"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/iv_increment"
app:layout_constraintStart_toEndOf="#id/iv_decrement"
app:layout_constraintTop_toTopOf="parent"
tools:text="1" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<data >
<import type="***.databinding.ProductDetailsQuantityLayoutBinding"/>
<import type="android.view.View" />
<variable
name="productQty"
type="String" />
<variable
name="increaseBtnClick"
type="android.view.View.OnClickListener" />
<variable
name="decreaseBtnClick"
type="android.view.View.OnClickListener" />
</data>
</layout>
Using the include tag, you can pass variables.
<include android:id="#+id/secondary"
layout="#layout/data_binding_included_secondary_layout"
bind:secondaryUser="#{mainUser}"/>
Ref:
https://medium.com/#elia.maracani/android-data-binding-passing-a-variable-to-an-include-d-layout-3567099b58f
So if you have an include tag in your Library module, then override that xml file in your resources, you may be able to change it.
Ref:
Override resources in library android
Alternatively, you can programmattically add a view into your XML, it should work if you use the correct viewbing tags:
Ref:
Programmatically adding a layout + children
You can use class attribute to avoid cast exception.
https://developer.android.com/topic/libraries/data-binding/generated-binding#custom_binding_class_names
However, It seems the binding class is still from library project class.
So, we can't override binding behavior. we can't add|remove binding elements from base library project xml.
we can customize element only that area unrelated to data binding.

Android: Does binding variable and passing to include tag layout work?

I have a problem with databinding. In the documentation, it said we can use include tag to host a custom layout and passing binding variable to it. When I tryout on 4.1.2 phone and emulator, the data not seems to bind but only bind the main layout fields.
This is my code of the main layout:
<layout>
<data>
<variable
name="Job"
type="nz.co.certifi.CERTIFI.Model.JobModel" />
</data>
<ScrollView
android:background="#color/TransparentColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/scrollView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="false"
android:layout_alignParentEnd="false"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true">
<RelativeLayout
android:background="#color/TransparentColor"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
app:Job="#{Job}"
android:id="#+id/layoutCertification"
layout="#layout/view_certification_control"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>
This is the layoutCertification:
<layout>
<data>
<variable
name="Job"
type="nz.co.certifi.CERTIFI.Model.JobModel" />
<variable
name="Form"
type="nz.co.certifi.CERTIFI.Model.FormROIModel" />
</data>
<nz.co.certifi.CERTIFI.Control.EditTextWithModel
xmlns:sparkNS="http://schemas.android.com/apk/res/nz.co.certifi.CERTIFI"
sparkNS:modelProperty="CertificateId"
sparkNS:modelType="JobModel"
sparkNS:validationType="required_only"
android:contentDescription="Job: Form Certificate Id"
sparkNS:errorRequiredMessage="#string/error_reference_no_required"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/activity_roi_step_one_hint_reference_no"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#{Job == null? Form.certificateId : Job.certificateId}"
android:textStyle="bold"
android:id="#+id/txtReferenceNo"
android:layout_alignParentTop="false"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/btnReference"
android:layout_toStartOf="#+id/btnReference"
android:layout_centerVertical="true" />
Yes it does. http://developer.android.com/tools/data-binding/guide.html#includes
Main layout
<data>
<variable
name="plu"
type="org.test.test.viewmodels.PluDetailViewModel" />
</data>
.
.
.
<include
layout="#layout/keypad_pludetail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
bind:plu="#{plu}"
/>
Included layout
<data>
<variable
name="plu"
type="org.test.test.viewmodels.PluDetailViewModel" />
</data>
.
.
.
<Button
android:id="#+id/keypad_accept"
style="#style/KeyPadButton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="#string/keypad_accept"
android:enabled="#{plu.isOK}"
android:onClick="#{plu.confirm}"
/>
In fragment
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_plu_details, container, false);
binding.setPlu(pluDetailViewModel);
binding.executePendingBindings();

Categories

Resources