2

this is my xml :

<data>

    <variable
        name="notificationViewmodel"
        type="com.kdcos.contsync.viewmodel.notification.NotificationViewModel"></variable>

</data>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorpalegrey">

    <RelativeLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_gravity="center"
        android:background="#689F38"
        android:gravity="center">

        <include
            android:id="@+id/layout_toolbar"
            layout="@layout/toolbar_white_bg" />
    </RelativeLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/header"
        android:layout_marginTop="20dp"
        android:background="@drawable/topbottomborder"
        android:orientation="vertical">


        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="18dp"
            android:paddingBottom="20dp"
            android:paddingLeft="@dimen/margin_20dp">


            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="sans-serif-medium"
                android:lineSpacingExtra="30sp"
                android:text="@={notificationViewmodel.getText()}"
                android:textColor="@color/colorDusk"
                android:textSize="16sp"
                android:textStyle="normal" />

            <Switch
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:paddingRight="10dp"
                android:onCheckedChanged="@={notificationViewmodel.checked}"
                android:textColor="@android:color/black" />

        </RelativeLayout>
    </LinearLayout>


</RelativeLayout>

this is my View Model

class NotificationViewModel(val context: Context, val bus: Bus, val manager: CNotificationManager) : BaseObservable() {
    private var checked: Boolean = false
    fun getToolbarTitle(): String? {
        return manager.getToolbarTitle()
    }

    fun setToolbarTextColor(): Int {
        return ContextCompat.getColor(context, R.color.colorDusk)
    }

    fun isChecked(): Boolean {
        return checked
    }

    fun setChecked(checked: Boolean) {
        this.checked = checked
    }

    fun getText(): String {
        if (isChecked()) {
            return "Notfication On"
        } else {
            return "Notification Off"
        }
        notifyChange()
    }
}

I want to apply data binding in switch so that when I on switch then textView should display Notification off and checked value should set to true when I off then it textview should display notification off and checked Boolean variable should set false. please suggest me how I will implement using Data-binding .

yogesh lokhande
  • 1,245
  • 1
  • 11
  • 20
Deven Dcostra
  • 31
  • 1
  • 3
  • try [this](https://stackoverflow.com/questions/37268572/how-can-we-implement-data-binding-for-switch-button-for-oncheckedchagelistener-e/39840609#39840609) – Maddy Dec 12 '17 at 11:39
  • Possible duplicate of [How to use data binding for Switch onCheckedChageListener event?](https://stackoverflow.com/questions/37268572/how-to-use-data-binding-for-switch-oncheckedchagelistener-event) – clauub Apr 12 '19 at 10:53

2 Answers2

2

You need to do it like this:

Inside xml:

android:onCheckedChanged="@{viewModel::executeOnStatusChanged}"
android:checked="@={viewModel.isChecked()}"

Inside Your view model:

@Bindable
val isChecked: MutableLiveData<Boolean> = MutableLiveData()

fun executeOnStatusChanged(switch: CompoundButton, isChecked: Boolean) {
     Timber.d("Status changed")
}
Hamza Maqsood
  • 355
  • 2
  • 11
1
android:onCheckedChanged="@={notificationViewModel.executeOnChanged()}"
android:isChecked="@={notificationViewModel.isChecked}"
Boken
  • 4,825
  • 10
  • 32
  • 42
Ankit Kumar
  • 3,663
  • 2
  • 26
  • 38
  • While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Adrian Mole Sep 21 '20 at 18:55