i am new in data binding, is there any idea how to apply click event on toolbar navigation icon directly in xml using data binding.
my xml is somthing 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"
xmlns:tools="http://schemas.android.com/tools"
xmlns:bind="http://schemas.android.com/apk/res-aut">
<data class="SelectCuisineBinding">
<variable
name="viewModel"
type="com.aman.camellia.kniterider.viewmodel.SelectCuisineViewModel"/>
<variable
name="activity"
type="com.aman.camellia.kniterider.view.activity.SelectCuisineActivity"/>
</data>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.aman.camellia.kniterider.view.activity.SelectCuisineActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
app:navigationIcon="?homeAsUpIndicator"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:title="@string/Cuisines"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_select_cuisine"
app:viewModel="@{viewModel}"
app:activity="@{activity}"/>
</android.support.design.widget.CoordinatorLayout>
</layout>
how to handle clickevent in viewholder. is there any idea...thanks
Android by default provides a binding adapter or support to add click listener. We just need to modify the attribute app:navigationOnClickListener. And the namespace is xmlns:app="http://schemas.android.com/apk/res-auto"
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/blue_grey_600"
app:navigationIcon="@drawable/ic_close_icon_resized"
app:title="@{viewModel.toolBarText}"
app:titleTextAppearance="@style/EmiToolbar.Text"
app:navigationOnClickListener="@{() -> viewModel.onToolbarNavigationClick()}"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
As i think you can apply click event on toolbar navigation icon using binding adapter. here your toolbar xml code ..
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
app:navigationIcon="?homeAsUpIndicator"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:title="@string/Cuisines"
app:onNavigationBackClick="@{1}"
app:popupTheme="@style/AppTheme.PopupOverlay" />
i apply BindingAdapter as app:onNavigationBackClick="@{1}" and return any parameter what you want.
And in your viewmodel add a bindigAdapter as i adding
public class YourViewModel ....
@BindingAdapter("onNavigationBackClick")
public static void onnavigationClicked(Toolbar toolbar,int b){
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do here what you want..
}
});
}
}
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