Why does changing the text view in the internal layout with viewbinding not reflect the text in the UI?

51 views Asked by At

I want to change text of TextView which is in inner ConstraintLayout.

When I try binding.lyricViewTitle.text = it.title, binding.lyricViewTitle text is changed(I check with Debug), but lyricViewTitle text is not updated at UI.

When I try binding.lyricView.findViewById<TextView>(R.id.lyric_view_title).text = it.title, text is changed and UI also is updated.

What's the difference? Is there any inner logic in ViewBinding?

// activity_mail.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:id="@+id/constraint_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.media3.ui.PlayerControlView
        android:id="@+id/player_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:controller_layout_id="@layout/custom_exo_player_control_view"
        app:hide_on_touch="false"
        app:repeat_toggle_modes="one"
        app:show_shuffle_button="true"
        app:show_timeout="0" />


    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/lyric_view"
        android:layout_width="0dp"
        android:animateLayoutChanges="true"
        android:layout_height="0dp"
        android:background="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/player_view">

        <TextView
            android:id="@+id/lyric_view_title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="15dp"
            android:ellipsize="end"
            android:maxLines="1"
            android:textSize="24sp"
            android:textStyle="bold"
            app:layout_constraintEnd_toStartOf="@id/lyric_view_close"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="Love wins all Love wins all Love wins all " />

        ( ... omit ... )


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

There are 1 answers

2
usr153.K On

I don't think there's any difference. But, I think <layout> is needed. I couldn't reproduce nor build without it.

Binding class is just an android view mapping class. binding.lyricView.findViewById<TextView>(R.id.lyric_view_title) and binding.lyricViewTitle are pointing the same view object.

Android view system is hierarchical so findingView finds a view from parent. And it costs a lot. So binding class was introduced in Android.

<?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"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraint_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/lyric_view"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:animateLayoutChanges="true"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">

            <TextView
                android:id="@+id/lyric_view_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>
class MainActivity: AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

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