Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply click event on navigation Icon of toolbar in xml using data binding

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

like image 928
Abid Khan Avatar asked Jan 27 '26 06:01

Abid Khan


2 Answers

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" />
like image 112
Mohanakrrishna Avatar answered Jan 30 '26 01:01

Mohanakrrishna


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..
            }
        });
    }
}
like image 31
user9348691 Avatar answered Jan 29 '26 23:01

user9348691