Android Programming The Big Nerd Ranch Guide by Bill Phillips, Chris Stewart, Kristin Marsicano (z-lib.org)

(gtxtreme123) #1

Chapter 24  More About Intents and Tasks


Setting Up NerdLauncher


Create a new Android application project named NerdLauncher. Select Phone and Tablet as the
form factor and API 19: Android 4.4 (KitKat) as the minimum SDK. Create an empty activity named
NerdLauncherActivity.


NerdLauncherActivity will host a single fragment and in turn should be a subclass of
SingleFragmentActivity. Copy SingleFragmentActivity.java and activity_fragment.xml into
NerdLauncher from the CriminalIntent project.


Open NerdLauncherActivity.java and change NerdLauncherActivity’s superclass to
SingleFragmentActivity. Remove the template’s code and override createFragment() to return a
NerdLauncherFragment. (Bear with the error caused by the return line in createFragment(). This will
be fixed in a moment when you create the NerdLauncherFragment class.)


Listing 24.1  Another SingleFragmentActivity


(NerdLauncherActivity.java)


public class NerdLauncherActivity extends SingleFragmentActivityAppCompatActivity {


@Override
protected Fragment createFragment() {
return NerdLauncherFragment.newInstance();
}


@Override
protected void onCreate(Bundle savedInstanceState) {
/ Auto-generated template code... /
}
}


NerdLauncherFragment will display a list of application names in a RecyclerView. Add the
RecyclerView library as a dependency, as you did in Chapter 8.


Rename layout/activity_nerd_launcher.xml to layout/fragment_nerd_launcher.xml to create a
layout for the fragment. Replace its contents with the RecyclerView shown in Figure 24.2.


Figure 24.2  Creating NerdLauncherFragment layout
(layout/fragment_nerd_launcher.xml)

Free download pdf