Integrating Fragments
As we saw at the outset, there will be three main elements of the user interface when we are done:
- a list of to-do items
- a place to edit an item, whether that is a new one being added to the list or modifying an existing one
- a place to view details of a single item
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.
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:
- Alt-Enter (Option-Return on macOS) will bring up context-aware “quick-fixes” for the problem at the code where the cursor is.
- Ctrl-Alt-O (Command-Option-O on macOS) will organize your Java
import
statements, including removing unused imports. - Ctrl-Alt-L (Command-Option-L on macOS) will reformat the Kotlin or XML in the current editing window, in accordance with either the default styles in Android Studio or whatever you have modified them to in Settings.
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:
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:
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.