Build Another Fragment (Game)

Kotlin and Android Development featuring Jetpack — by Michael Fazio (20 / 125)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Add Data Binding to the App | TOC | Summary and Next Steps 👉

We created GameFragment last chapter, but it still looks pretty sad, especially compared to the PickPlayersFragment. By the time we wrap up this chapter, we’ll have a screen that looks like the image shown.

images/pd.fragments/game-fragment-coin-slots.png

The rest of this chapter will be spent in GameFragment, so I recommend going to navigation/nav_graph.xml and setting app:startDestination to @id/gameFragment. Then, we’ll start by enabling data binding for GameFragment.

Enable Data Binding for GameFragment

As we’re just turning on data binding in GameFragment right now, all we need to do in fragment_game.xml is add that generic <layout> tag plus an empty <ConstraintLayout>. While the <ConstraintLayout> isn’t critical for data binding, we’re going to be using it soon, so might as well add it now. Our bare-bones view’s XML will look like this:

​ ​<?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"​>

​…

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.