Starting CriminalIntent

In this chapter, you are going to start on the detail part of CriminalIntent. Figure 8.4 shows you what CriminalIntent will look like at the end of this chapter.

Figure 8.4  CriminalIntent at the end of this chapter

CriminalIntent at the end of this chapter

The screen shown in Figure 8.4 will be managed by a UI fragment named CrimeFragment. An instance of CrimeFragment will be hosted by an activity named MainActivity.

For now, think of hosting as the activity providing a spot in its view hierarchy to contain the fragment and its view (Figure 8.5). A fragment is incapable of getting a view onscreen itself. Only when it is inserted in an activity’s hierarchy will its view appear.

Figure 8.5  MainActivity hosting a CrimeFragment

MainActivity hosting a CrimeFragment

CriminalIntent will be a large project, and one way to keep your head wrapped around a project is with an object diagram. Figure 8.6 gives you the big picture of CriminalIntent. You do not have to memorize these objects and their relationships, but it is good to have an idea of where you are heading before you start.

You can see that CrimeFragment will do the sort of work that your activities did in GeoQuiz: create and manage the UI and interact with the model objects.

Figure 8.6  Object diagram for CriminalIntent (for this chapter)

Object diagram for CriminalIntent (for this chapter)

Three of the classes shown in Figure 8.6 are classes that you will write: Crime, CrimeFragment, and MainActivity.

An instance of Crime will represent a single office crime. In this chapter, a crime will have a title, an ID, a date, and a Boolean that indicates whether the crime has been solved. The title is a descriptive name, like Toxic sink dump or Someone stole my yogurt! The ID will uniquely identify an instance of Crime.

For this chapter, you will keep things very simple and use a single instance of Crime. CrimeFragment will have a property (crime) to hold this isolated incident.

MainActivity’s view will consist of a FrameLayout that defines the spot where the CrimeFragment’s view will appear.

CrimeFragment’s view will consist of a LinearLayout with a few child views inside of it, including an EditText, a Button, and a CheckBox. CrimeFragment will have a property for each of these views and will set listeners on them to update the model layer when there are changes.

Creating a new project

Enough talk; time to build a new app. Create a new Android application (FileNewNew Project...). Select the Empty Activity template (Figure 8.7). Click Next.

Figure 8.7  Creating the CriminalIntent application

Creating the CriminalIntent application

Configure your project as shown in Figure 8.8. Name the application CriminalIntent. Make sure the Package name is com.bignerdranch.android.criminalintent and the Language is Kotlin. Select API 21: Android 5.0 (Lollipop) from the Minimum API level dropdown. Finally, check the box to Use AndroidX artifacts.

Figure 8.8  Configuring the CriminalIntent project

Configuring the CriminalIntent project

Click Finish to generate the project.

Before proceeding with MainActivity, create the model layer for CriminalIntent by writing the Crime class.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
54.89.24.147