Android XML Data Binding pass view by id - android

In order to show a BottomSheetBehavior i need to pass his view like below.
ViewUtil.kt class
fun showBottomSheet(view: View) {
val bottomSheetBehavior = BottomSheetBehavior.from(view)
if (bottomSheetBehavior.state == BottomSheetBehavior.STATE_COLLAPSED) {
bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
} else {
bottomSheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED
}
}
Fragment.kt
termsTitle.setOnClickListener {
showBottomSheet(tosLayoutBottomSheet)
}
bottom_sheet_tos.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
android:id="#+id/tosLayoutBottomSheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:behavior_hideable="true"
app:behavior_peekHeight="0dp"
app:layout_behavior="#string/bottom_sheet_behavior">
<!-- some stuff about terms of service -->
</androidx.constraintlayout.widget.ConstraintLayout>
fragment.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:ignore="MissingDefaultResource">
<data>
<import
alias="v"
type="android.view.View" />
<variable
name="fragmentViewModel"
type=".FragmentViewModel"/>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background_main_page">
<TextView
android:id="#+id/terms_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="24dp"
android:layout_marginBottom="8dp"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:text="#string/terms_of_service"
android:textColor="#color/colorGrey"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
So, instead use onclicklistener from fragment I would like to do it from my xml and viewmodel like:
fragment.xml
<TextView
android:id="#+id/terms_title"
android:onClick="#{(v)-> fragmentViewModel.showBottomSheetFrag(tosLayoutBottomSheet)}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="24dp"
android:layout_marginBottom="8dp"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:text="#string/terms_of_service"
android:textColor="#color/colorGrey"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" />
I know the above approach not work because tosLayoutBottomSheet id is in another layout. How can I pass an id from a different layout ?

A bit late but you can add an import of R class in your xml file and then pass the view id with R.id.view_id
<import type="com.xxx.xxxx.R" />
then in you onclick
android:onClick="#{(v)->view.yourMethod(R.id.view_id)}"

Related

Problem setting color in toolbar (android-kotlin)

I'm having a problem with my toolbar.
I want the device app and toolbar to be the same color, but don't use findviewbyid. (I'm migrating the entire project just to view binding
)
OK behavior (using findviewbyid).
NOK behavior (using view binding), is giving a little misalignment of the textView, it seems to receive a margin that was not placed.
But if I declare different in onWindowsInsetsChanged method or don't use it, I get another problem, the color is not correct.
Using findviewbyid:
override fun onWindowsInsetsChanged(view: View, insets: WindowInsets, padding: InitialPadding) {
with(binding) {
val toolbar = fragmentEditObjectiveNameRoot.findViewById<View>(R.id.toolbar)
toolbar.updatePadding(top = topInset)
editObjectiveNameRoot.updatePadding(
bottom = insets.systemWindowInsetBottom
)
}
}
Using view binding:
override fun onWindowsInsetsChanged(view: View, insets: WindowInsets, padding: InitialPadding) {
with(binding) {
//calling the include toolbar and the root of the toolbar fragment
toolbar.viewImageToolbar.updatePadding(
top = topInset
)
editObjectiveNameRoot.updatePadding(
bottom = insets.systemWindowInsetBottom
)
}
}
Different declaration in onWindowsInsetsChanged:
override fun onWindowsInsetsChanged(view: View, insets: WindowInsets, padding: InitialPadding) {
with(binding) {
//calling root from fragment and instead of calling the include toolbar and its root
fragmentEditObjectiveNameRoot.updatePadding(
top = topInset
)
editObjectiveNameRoot.updatePadding(
bottom = insets.systemWindowInsetBottom
)
}
}
I would like to leave the same color behavior when using findviewbyid, but with view binding, but without losing textView alignment.
Here is the xml that receives the toolbar include:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/fragmentEditObjectiveNameRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/editObjectiveNameRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="#+id/bgColorFrameLayout"
android:layout_width="match_parent"
android:layout_height="#dimen/color_frame_layout_height"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/toolbar" />
<androidx.core.widget.NestedScrollView
android:id="#+id/scrollView"
android:layout_width="0dp"
android:layout_height="0dp"
android:fillViewport="true"
android:clipToPadding="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/toolbar">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="#dimen/screen_margin_horizontal"
android:layout_marginTop="18dp"
android:layout_marginEnd="#dimen/screen_margin_horizontal"
android:paddingBottom="16dp">
<com.google.android.material.card.MaterialCardView
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="#color/bari_white"
app:cardCornerRadius="#dimen/card_corner_radius"
app:cardElevation="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/card_padding">
<TextView
android:id="#+id/titleSubAccount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/sub_account_control_settings_edit_name"
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<br.com.bancobari.core_ui.views.text_input.BariTextInputLayout
android:id="#+id/nameInputLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
app:helperIconEnabled="true"
app:helperStart_enabled="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/titleSubAccount">
<br.com.bancobari.core_ui.views.text_input.BariTextInputEditText
android:id="#+id/nameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName|textCapWords"
android:maxLength="16"
android:textAlignment="center"
android:textSize="16sp" />
</br.com.bancobari.core_ui.views.text_input.BariTextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
<br.com.bancobari.core_ui.views.SubmitButton
android:id="#+id/saveButton"
style="#style/DefaultButton.Icon.Arrow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="#string/objective_settings_save"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/cardView"
app:layout_constraintVertical_bias="1.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
<include
android:id="#+id/toolbar"
layout="#layout/view_image_toolbar" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Here the toolbar xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="#+id/viewImageToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:minHeight="#dimen/custom_toolbar_height"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<View
android:layout_width="0dp"
android:layout_height="#dimen/custom_toolbar_height" />
</androidx.appcompat.widget.Toolbar>
<TextView
android:id="#+id/iconEmojiView"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginEnd="10dp"
android:gravity="bottom"
android:textSize="22.5sp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="#id/toolbarTitleTextView"
app:layout_constraintEnd_toStartOf="#+id/toolbarTitleTextView"
tools:visibility="visible" />
<TextView
android:id="#+id/toolbarTitleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:ellipsize="end"
android:fontFamily="#font/nunito_bold"
android:gravity="center"
android:lines="1"
android:textColor="#color/text_neutral_gray_800"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="#id/viewImageToolbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Teste" />
<ImageView
android:id="#+id/toolbarRightIconImageView"
style="#style/Widget.AppCompat.ActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_settings"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="#id/toolbarTitleTextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#id/toolbarTitleTextView" />
</androidx.constraintlayout.widget.ConstraintLayout>
In your first block of code with findViewById, this
val toolbar = fragmentEditObjectiveNameRoot.findViewById<View>(R.id.toolbar)
returns the ConstraintLayout at the root of your included layout with the ID toolbar.
The property toolbar in your binding is another binding for the included layout rather than the root view of that layout. The root property of that included binding in this case is the same ConstraintLayout from above, so you should use instead:
toolbar.root.updatePadding(
top = topInset
)
What you were doing was changing the padding of an inner view of your complete Toolbar layout, the viewImageToolbar element.

Why is Android Studio ScrollView, having a resource compilation fail

I am fairly new with Android Studio and have ran into a problem that I have not come across before. Within an xml layout I am having a ParseError on line 22, which is the layout_height for the ScrollView. The error says Resource Compilation Failed. I have checked any posts that are even remotely close to this issue and have had no luck so far. Any tips would be appreciated.
<?xml version="1.0" encoding="utf-8"?>
<layout android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SizeFragment"
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="orderModel"
type="com.example.pizzaorder.model.OrderModel" />
<variable
name="sizeFragment"
type="com.example.pizzaorder.SizeFragment" />
</data>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textview.MaterialTextView
android:id="#+id/welcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="120dp"
android:gravity="center"
android:text="#string/welcome"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/twelve_inch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="72dp"
android:layout_marginEnd="32dp"
android:onClick="#{() -> sizeFragment.moveToBase(12)}"
android:text="#string/size_twelve"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/welcome" />
<Button
android:id="#+id/eighteen_inch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="32dp"
android:onClick="#{() -> sizeFragment.moveToBase(18)}"
android:text="#string/size_eighteen"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/twelve_inch" />
<Button
android:id="#+id/twenty_four_inch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="32dp"
android:onClick="#{() -> sizeFragment.moveToBase(24)}"
android:text="#string/size_twenty_four"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/eighteen_inch" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</layout>
As per the comment, remove height, width, and context from the layout tag and put it in the ScrollView tag. The layout tag does not support these attributes, it is only used to create the data binding and to hold the variables.

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

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.

ConstraintLayout unnecessary error

I'm using ConstraintLayout wrapped with layout tags for dataBinding, all layouts works fine in execution and runtime, but I got an annoying issue:
As you see, constraint id's say "Cannot resolve symbol '#+id/tvAccount'", seemed as errors when trying to commit, but there's nothing wrong with workflow or the app, and all works fine as intended. It's same in all xml files. Cleaning, invalidating cache etc. is not fixing it. Has anyone met the same issue ?
Edit:
XML code for splash, it'll say "Cannot resolve symbol '#+id/imageViewSplash'" when you hover on the app:layout_constraintTop_toBottomOf="#+id/imageViewSplash" :
<?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">
<!--suppress AndroidUnknownAttribute -->
<data class="SplashBinding"/>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.splash.SplashFragment">
<ImageView
android:id="#+id/imageViewSplash"
android:layout_width="wrap_content"
android:layout_height="39dp"
android:layout_marginBottom="8dp"
android:contentDescription="#string/content_description_splash_icon"
android:src="#mipmap/splash_icon"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<ImageView
android:id="#+id/imageViewSplashLoader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="16dp"
android:contentDescription="#string/content_description_splash_loading"
android:src="#mipmap/splash_loader"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageViewSplash"/>
</android.support.constraint.ConstraintLayout>
</layout>
Edit 2:
It's same error when you hover on app:layout_constraintTop_toBottomOf="#+id/imageViewSplash", even without <layout> tags:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.splash.SplashFragment">
<ImageView
android:id="#+id/imageViewSplash"
android:layout_width="wrap_content"
android:layout_height="39dp"
android:layout_marginBottom="8dp"
android:contentDescription="#string/content_description_splash_icon"
android:src="#mipmap/splash_icon"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<ImageView
android:id="#+id/imageViewSplashLoader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="16dp"
android:contentDescription="#string/content_description_splash_loading"
android:src="#mipmap/splash_loader"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageViewSplash"/>
</android.support.constraint.ConstraintLayout>
After keeping layout in the editor i might have got the solution for you. The possible cause here is might be with the xmlns:bind and xmlns:android
Please try following it's not showing me any error.
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:bind="http://schemas.android.com/apk/res/android">
<data></data>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tvAccount"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="13dp"
android:layout_marginEnd="32dp"
android:text="Account"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tvPhoneValidationTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Phone Validation"
app:layout_constraintEnd_toEndOf="#id/tvAccount"
app:layout_constraintStart_toStartOf="#id/tvAccount"
app:layout_constraintTop_toBottomOf="#id/tvAccount" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
I've resolved the same issue by moving the app namespace from <layout/> to <ConstraintLayout/> tag.
Before:
<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
...>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
After:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
...>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

Categories

Resources