Day72 of #100DaysOfCode

Kushagra Kesav
2 min readApr 19, 2022

--

Day72 of #100DaysOfCode

Hii folks 🙌

Today I will be continuing the same pathway in which we’ll use the LiveData with data binding

Unit 3: Navigation

Pathway 2: Architecture componentsSource

https://developer.android.com/courses/android-basics-kotlin/course

View Binding is a feature that allows us to more easily access views in code. It generates a binding class for each XML layout file. An instance of a binding class contains direct references to all views that have an ID in the corresponding layout. For example, the Unscramble app currently uses view binding, so the views can be referenced in the code using the generated binding class.

binding.textViewUnscrambledWord.text = newWord
binding.score.text = getString(R.string.score, newScore)
binding.wordCount.text =
getString(R.string.word_count, newWordCount, MAX_NO_OF_WORDS)

Data Binding Library is also a part of the Jetpack library. Data Binding binds the UI components in our layouts to data sources in our app using a declarative format.

binding.textViewUnscrambledWord.text = viewModel.currentScrambledWordandroid:text="@{gameViewModel.currentScrambledWord}"

We will change view binding to data binding

  • In the build.gradle(Module) file, enable the dataBinding property under the buildFeatures section.
  • We will include the required plugins
  • Then we will convert the layout file to a data binding layout.
<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>

</data>

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.constraintlayout.widget.ConstraintLayout
...
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</layout>
  • In GameFragment, at the beginning of the onCreateView() method, we will change the instantiation of the binding variable to use data binding.

Replace

binding = GameFragmentBinding.inflate(inflater, container, false)

With

binding = DataBindingUtil.inflate(inflater, R.layout.game_fragment, container, false)

That is all for Day72 ✅

Thanks for reading, See you tomorrow!

If you are reading my #100Days Journey, feel free to drop by ;)

--

--

Kushagra Kesav
Kushagra Kesav

Written by Kushagra Kesav

Developer Relations | Communities | Software Engineering | https://www.linkedin.com/in/kushagrakesav

No responses yet