Home > other >  How can i use DataBinding for TextView?
How can i use DataBinding for TextView?

Time:04-24

i have a problem, i dont know how to use DataBinding for TextView. i made a DataBinding for Glide and it success, but for TextView i dont know how to do it

this is the code

viewModel2.shouldShowImageProfile.observe(this) {
        Glide.with(binding.root)
            .load(it)
            .circleCrop()
            .into(binding.rivProfile)
    }

    viewModel2.shouldShowUsername.observe(this){
        TextView.with(binding.root) <- the TextView
            .load(it)
            .into(binding.tvHello)
    }

CodePudding user response:

I wrote my answer making an assumption about shouldShowUsername, but as that's your String value, you can do something like this:

viewModel2.shouldShowUsername.observe(this) { shouldShowUsername ->
    binding.tvHello.isVisible = !shouldShowUsername.isNullOrEmpty()
    binding.tvHello.text = shouldShowUsername
}

That'll cause your TextView to only be displayed when shouldShowUsername has a proper value then take that value and assign it to the TextView. If you want the TextView to always show up, you can skip that first line within the block.

CodePudding user response:

If you dont want to use observe method on variables of viewmodel you can use data binding and change value of textview from the xml directly, so that you dont have to manually write binding.tvUserName.text = it.value. Below code shows example for usuage of data binding with xml and viewmodel.
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">

    <data>

        <variable
            name="viewModel"
            type="com.example.kotlinbasics.android_architecture.mvvm_architecture.ChatViewModel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_red_light"
        tools:context=".android_architecture.mvvm_architecture.UserOneFragment">

        <androidx.appcompat.widget.AppCompatEditText
            android:id="@ id/etMessageToSecond"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/_10sdp"
            android:background="@drawable/text_field_background"
            android:gravity="center"
            android:hint="@string/enter_message"
            android:padding="@dimen/_7sdp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintWidth_percent="0.7" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@ id/tvCurrentMessage"
            android:text="@{`Current Message: ` viewModel.latestMessageFromFirst}"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/_50sdp"
            android:padding="@dimen/_10sdp"
            android:textColor="@color/white"
            android:textSize="@dimen/_20ssp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/etMessageToSecond" />

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

In the above example I am printing the live text in textview which user writes in the edittext field. Whenever user writes in edittext, its value gets updated in the viewmodel and textview access that value on runtime.
For image thing, you can do like, you can create custom binding adapter which updates the image. Example:

<androidx.appcompat.widget.AppCompatImageView
            android:id="@ id/ivEmailVerification"
            loadImageFromUrl="@{viewModel.emailVerifyImageUrl}"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tvIsEmailValid"
            app:layout_constraintWidth_percent="0.7" />

and on BindingAdpater

@BindingAdapter("loadImageFromUrl")
fun ImageView.loadImageFromUrl(imageUrl: String) {
    Picasso.get().load(imageUrl).into(this)
}
  • Related