Search code examples
androidkotlinandroid-databindingandroid-viewbinding

If I add databinding to gradle, is it possible to use viewbinding as well?


I learned today that there is something called viewbinding other than databinding.

I was new to viewbinding, but I was already using it in code.

In other words, I knew it was data binding and was using it wrong.

To use viewbinding i need to apply below code in gradle. However, I have never applied anything other than data binding.

dataBinding { enabled = true }

However, in my code, it is accessed through the ID of the view with binding.title.text-like code. There is also no <data> tag. Isn't this a view binding?

XML

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".fragment.WritingRoutineFragment">
        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/Theme.AppBarOverlay"
            app:elevation="0dp">
            <androidx.appcompat.widget.Toolbar
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView
                    android:id="@+id/title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:textColor="@color/white"
                    android:textAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
                    android:layout_centerHorizontal="true" />
            </androidx.appcompat.widget.Toolbar>
        </com.google.android.material.appbar.AppBarLayout>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>

WRFragment.kt

class WritingRoutineFragment : Fragment() {
    
    var titleArg: String? = null
    private var _binding: FragmentWritingRoutineBinding? = null
    private val binding get() = _binding!!
    private val viewModel = WriteRoutineViewModel()
    
    override fun onCreateView(inflater: LayoutInflater,
                              container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        _binding = FragmentWritingRoutineBinding.inflate(inflater, container, false)
        viewModel.setTitle(titleArg)

        viewModel.title.observe(viewLifecycleOwner) { titleData ->
            // UI UPDATE
            binding.title.text = titleData // viewbinding? title is TextView ID
        }

        return binding.root
    }
}

Solution

  • viewModel.title.observe(viewLifecycleOwner) { titleData ->
                // UI UPDATE
                binding.title.text = titleData // viewbinding? title is TextView ID
            }
    

    yes,you are using view binding.

    if you want to use data binding ,code will be like

        <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto">
     <data>
            <variable
                name="viewModel"
                type="yourpackage.viewmodel" />
        </data>
        <androidx.coordinatorlayout.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".fragment.WritingRoutineFragment">
            <com.google.android.material.appbar.AppBarLayout
                android:id="@+id/appbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/Theme.AppBarOverlay"
                app:elevation="0dp">
                <androidx.appcompat.widget.Toolbar
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <TextView
                        android:id="@+id/title"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:textColor="@color/white"
                        android:text="@{viewModel.title}"
                        android:textAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
                        android:layout_centerHorizontal="true" />
                </androidx.appcompat.widget.Toolbar>
            </com.google.android.material.appbar.AppBarLayout>
        </androidx.coordinatorlayout.widget.CoordinatorLayout>
    </layout>