Build a Fragment (Pick Players)

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

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Chapter 2 Build Views with Fragments | TOC | Add Data Binding to the App 👉

Throughout this book, we’re going to be using <ConstraintLayout> view groups as the main containers for most of our screens. We could handle things a few ways, but <ConstrainLayout> is a great way to get things right where you want them on each screen while still allowing for the flexibilty needed for multiple device types. In fragment_pick_players.xml, we can replace the entire existing <FrameLayout> tag with this new <ConstraintLayout>. The XML will then look like this:

​ ​<?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:layout_width=​"match_parent"​
​ android:layout_height=​"match_parent"​
​ android:layout_margin=​"16dp"​
​ tools:context=​".fragments.PickPlayersFragment"​>

​ ​<!-- Content will eventually go in here. -->​

​ </androidx.constraintlayout.widget.ConstraintLayout>

If Android Studio is complaining that the xmlns:app attribute isn’t being used, that’s OK. It won’t be used until we start adding constraints to the views inside this <ConstraintLayout>.

--

--

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.