Step #1: Creating a Fragment

First, we need to set up a fragment. While Android Studio offers a new-fragment wizard, its results are poor, so we will create one as a normal Kotlin class.

Right-click over the com.commonsware.todo package in the java/ directory and choose “New” > “Kotlin File/Class” from the context menu. This will bring up a strange-looking popup where we can define a new Kotlin class:

Android Studio Create Kotlin Class Popup
Android Studio Create Kotlin Class Popup

For the name, fill in RosterListFragment, as this fragment is showing a list of our to-do items. Choose “Class” in the list of Kotlin structures below the field. Then, press Enter or Return to create the class. That will give you a RosterListFragment that looks like:

package com.commonsware.todo

class RosterListFragment {
}

Modify it to extend androidx.fragment.app.Fragment:

package com.commonsware.todo

import androidx.fragment.app.Fragment

class RosterListFragment : Fragment() {
}

Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.