Integrating Fragments

As we saw at the outset, there will be three main elements of the user interface when we are done:

We will use fragments to implement each of those. This lines up with current recommended practices in Android development, and it gives us the flexibility to rearrange those bits of UI in varying situations (e.g., show both the list and one of the other fragments at the same time on larger-screen devices). In this chapter, we start setting up the first of these fragments, to show the list of to-do items.

This is a continuation of the work we did in the previous tutorial. The book’s GitLab repository contains the results of the previous tutorial as well as the results of completing the work in this tutorial.

You can learn more about fragments in the "Adopting Fragments" chapter of Elements of Android Jetpack!

But First, Some Notes About Working with Kotlin

Starting in this tutorial, we will begin editing Kotlin source files. Some useful Android Studio shortcut key combinations are:

Copying and pasting Kotlin code from this book may or may not work, depending on what you are using to read the book. For the PDF, some PDF viewers (e.g., Adobe Reader) should copy the code fairly well; others may do a much worse job. The book’s preface has a section with recommended PDF viewers. Reformatting the code with Ctrl-Alt-L (Command-Option-L on macOS) after pasting it in sometimes helps.

Also, you may find it useful to have the IDE supply “hints” about the types it thinks that variables and function return values resolve to. Kotlin does not require you to enter all of the type information, as the compiler can infer types in many places. However, sometimes that makes it difficult to identify where things are going wrong. Enabling type hints allows the IDE to tell you the inferred types, without you having to enter those types yourself:

Android Studio Kotlin Editor, Showing Variable Type Hint
Android Studio Kotlin Editor, Showing Variable Type Hint

To toggle this on, go into the Settings dialog (“File” > “Settings” in Linux and Windows, and in “Android Studio” > “Preferences…” on macOS). Drill down into “Editor” > “Inlay Hints” > “Kotlin” in the category tree on the left, then check the hints that you want in the various lists:

Android Studio Settings, Showing Type Hints Options
Android Studio Settings, Showing Type Hints Options

And if you see a “Code Vision” checkbox with no caption, that is a bug.


Prev Table of Contents Next

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