Home > Mobile >  On Click event Does not working with Data Binding in Android Studio
On Click event Does not working with Data Binding in Android Studio

Time:09-20

I am learning DataBinding in android studio. But I am facing a problem with binding a ModelView. I want to bind a function with a button on click event. I set a function in the model view. I want to update my text view with on click event of my button. But When I click the button my text is not updating. I can not understand what I have done wrong.

XML layout:

<?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="model"
            type="com.example.jetpack.MainModelView" />

    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <TextView
            android:id="@ id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{model.title}"
            android:textSize="28sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@ id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="32dp"
            android:text="Update Text"
            android:onClick="@{()-> model.update()}"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/tv" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

Main Model View:

class MainModelView : ViewModel() {

    var title: String = " This is My Application"

    fun update() {
        title = "I am Changed"
        Log.d("UPDATE", "update successfully from main model view")
    }
}

Main Activity:

class MainActivity : AppCompatActivity() {

    lateinit var binding: ActivityMainBinding
    lateinit var mainModelView: MainModelView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main)

        mainModelView = ViewModelProvider(this).get(MainModelView::class.java)

        binding.model = mainModelView

    }

}

My app Image: enter image description here

thanks in advance for helping.

CodePudding user response:

The title variable in ViewModel needs to be a ObservableField or LiveData otherwise you xml will never know when it's value got updated –

class MainViewModel : ViewModel() { var text = MutableLiveData(" Welcome to my application ")

fun updateText() {
    text.value = " Text is updated successfully "
}
 }

class MainActivity : AppCompatActivity() {

lateinit var binding: ActivityMainBinding
lateinit var mainViewModel: MainViewModel

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = DataBindingUtil.setContentView(this, R.layout.activity_main)

    // Creating MainViewModel object
    mainViewModel = MainViewModel()

    // Binding mainViewModel variable 
    // with MainViewModel object
    binding.mainViewModel = mainViewModel 
    
    // passing LifeCycleOwner to binding object
    binding.lifecycleOwner = this 
}

}

  • Related