Ebook Android application development for dummies: Part 2 presents the following content: Chapter 9: designing the task reminder application; chapter 10: going a la carte with your menu; chapter 11: handling user input; chapter 12: getting persistent with data storage; chapter 13: reminding the user with alarmmanager; chapter 14: updating the android status bar; chapter 15: working with android’s preference framework; chapter 16: ten great free sample applications and SDKs (with code!); chapter 17: ten tools that make your developing life easier.
Part III Creating a Feature-Rich Application I In this part n Part III, I expand on the knowledge that you acquire in Part II by demonstrating how you can build a featurerich application I don’t trudge through every detail as I in Part II, but I expand on the details that you need to know to become a master Android application developer I also mention a few advanced topics that can help bridge the gap between beginner and advanced Android developer In this part, I showcase how and why you would create certain features to enhance users’ experiences with your application At the end of Part III, you will have a fully-functioning advanced application that interacts with a local database and custom preferences Chapter Designing the Task Reminder Application In This Chapter ▶ Listing the application’s requirements ▶ Developing multiple screens ▶ Building a list activity ▶ Working with intents B uilding Android applications is fun, but building truly in-depth applications is exciting because you dive into the real guts of the Android platform In this chapter, I introduce you to the Task Reminder application, which will be built from end to end over the next couple of chapters The Task Reminder application allows users to create a list of items that have a reminder time associated with each individual item Reviewing the Basic Requirements The Task Reminder application has a few basic requirements to fulfill what is expected of it: ✓ It must be able to accept user input — having a personalized task application that does not allow user input would be silly! ✓ The tasks must be easy to manage ✓ Each task must have a reminder date and time in which the user will be reminded of the task ✓ The user must be notified of the task when the reminder time has arrived ✓ Users must be able to delete tasks ✓ Users must be able to not only add tasks but to edit them 212 Part III: Creating a Feature-Rich Application You see a lot of interaction happening with the user and the Android system in this application Throughout the development of this application, you are introduced to various facets of Android development that can help you in your career I wish I would have known some of these things when I started; it would have saved me a lot of time! That’s alarming!: Scheduling a reminder script For the Task Reminder application to truly work, you need to implement some sort of reminder-based system As a fellow developer, the first thing that comes to mind is a scheduled task or cron job In the Windows operating system, developers can create a scheduled task to handle the execution of code/scripts at a given time In the UNIX/Linux world, developers can use cron (short for chronos — Greek for time) to schedule scripts or applications Because Android is running the Linux 2.6 kernel, it would be normal to assume that Android has a crontab you could edit Cron is driven by crontab, which is a configuration file that specifies the commands to run at a given time Unfortunately Android does not have cron; however, Android has the AlarmManager class, which achieves the same thing The AlarmManager class allows you to specify when your application should start in the future Alarms can be set as a single-use alarm or as a repeating alarm The Task Reminder application utilizes the AlarmManager to remind users of their tasks Storing data You will be exposed to many new features and tools in this application, and a big question that may be lingering in your head is, where am I going to put the activities, the task data, the alarms, and so on These items will be stored in the following locations: ✓ Activities and broadcast receivers: In one Java package ✓ Task data: SQLite database ✓ Alarm info: Pulled from the SQLite database and placed in the AlarmManager via the intent system Chapter 9: Designing the Task Reminder Application Distracting the user (nicely) After an alarm fires, you need to notify the user of the alarm The Android platform provides mechanisms to bring your activity to the foreground when the alarm fires, but that is not an optimal notification method because it steals focus from what the user was doing Imagine if the user was typing a phone number or answering a phone call and an alarm fired that brought an activity to the foreground Not only would the user be irritated, he most likely would be confused because an activity started that he did not initiate manually Therefore, you have various ways in which you can grab the user’s attention without stealing the main focus away from his current activity These mechanisms include the following: ✓ Toasts: A toast is a small view that contains a quick message for the user This message does not persist because it is usually available for only a few seconds at most A toast never receives focus I won’t use a toast for reminding the user, but instead I use a toast to notify the user when her activity has been saved so that she knows something happened ✓ Notification Manager: The NotificationManager class is used to notify a user that an event or events have taken place These events can be placed in the status bar, which is located at the top of the screen The notification items can contain various views and are identified by icons that you provide The user can slide the screen down to view the notification ✓ Dialog boxes: A final, not-so-popular method to grab a user’s attention is to open a dialog window that can immediately steal focus from the user’s currently running app and direct it to a dialog window While this may indeed work as a method for grabbing the attention of the user, the user may get irritated because your app is stealing focus (possibly on a constant basis if the user has a lot of reminders) from his current actions in another application I will be using the NotificationManager class to handle the alarms for the Task Reminder application Creating the Application’s Screens The Task Reminder application will have two different screens that perform all the basic CRUD (Create, Read, Update, and Delete) functions The first view is a list view that lists all the current tasks in the application, by name 213 214 Part III: Creating a Feature-Rich Application This view also allows you to delete a task by long-pressing the item The second view allows you to view (Read), add (Create), or edit (Update) a task Each screen eventually interacts with a database for changes to be persisted over the long-term use of the application Starting the new project To get started, Open Eclipse and create a new Android project with a Build Target of Android 2.2 and a MinSDKVersion of Provide it with a valid name, package, and activity The settings I have chosen are shown in Table 9-1 You may also choose to open the example Android project for Chapter provided by the online source code download This provides you with a starting point that has the same settings as my project Table 9-1 New Project Settings Property Value Project Name Task Reminder Build Target Android 2.2 (API Level 8) Application Name Task Reminder Package Name com.dummies.android taskreminder Create Activity ReminderListActivity Min SDK Version Note the Create Activity property value — ReminderListActivity Normally I give the first activity in an application the name of MainActivity; however, the first screen that the user will see is a list of current tasks Therefore, this activity is actually an instance of a ListActivity; hence the name — ReminderListActivity Creating the task list When working with ListActivity classes, I like to have my layout file contain the word list This makes it easy to find when I open the res/layout directory I’m going to rename the main.xml file located in the res/layout directory to reminder_list.xml To rename the file in Eclipse, you can either right-click the file and choose Refactor➪Rename or select the file and press Shift+Alt+R Chapter 9: Designing the Task Reminder Application After you change the filename, you need to update the name of the file in the setContentView() call inside the ReminderListActivity.java file Open the file and change the reference to the new filename you chose The ReminderListActivity class also needs to inherit from the ListActivity class instead of the regular base activity Make that change as well My new ReminderListActivity class looks like Listing 9-1 Listing 9-1: The ReminderListActivity Class public class ReminderListActivity extends ListActivity { /** Called when the activity is first created */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.reminder_list); } } Your ReminderListActivity references the reminder_list layout resource that currently contains the default code that was generated when you created the project To work with a ListActivity, you need to update this layout with new code, as shown in Listing 9-2 Listing 9-2: The reminder_list.xml Contents ➝5 ➝8 ➝ 11 This code is briefly explained as follows: ➝5 Defines a ListView, which is an Android view that is used to show a list of vertically scrolling items The ID of the ListView must be @id/android:list or @+id/android:list ➝8 Defines the empty state of the list If the list is empty, this is the view that will be shown When this view is present, the ListView will automatically be hidden because there is no data to display This view must have an ID of @id/android:empty or @+id/ android:empty 215 216 Part III: Creating a Feature-Rich Application ➝ 11 This line uses a string resource called no_reminders to inform the user that no reminders are currently in the system You need to add a new string resource to the res/values/strings.xml file with the name of no_reminders The value I’m choosing is “No Reminders Yet.” Creating and editing task activities The Task Reminder application needs one more screen that allows the user to edit a task and its information This screen will be all-inclusive, meaning that one single activity can allow users to create, read, and update tasks In Eclipse, create a new activity that can handle these roles I’m choosing to call mine ReminderEditActivity by right-clicking the package name in the src folder and choosing New➪Class or by pressing Shift+Alt+N and then choosing Class In the new Java class window, set the superclass to android.app.Activity and choose Finish A blank activity class now opens, and inside this class, type the following lines that are boldface: public class ReminderEditActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.reminder_edit); } } In line of the preceding code, I am setting the layout of the activity to the reminder_edit resource, which is defined in the next section This layout contains the various fields of the task in which the user can edit or create You also need to inform the Android platform about the existence of this activity by adding it to the Android Manifest You can so by adding it to the Application element of the ApplicationManifest.xml file, as shown here in boldface: Chapter 9: Designing the Task Reminder Application If you not add the activity to the ApplicationManifest.xml file, you receive a run-time exception informing you that Android cannot find the class (the activity) Creating the adding/editing layout The layout for adding and editing is fairly simple because the form contains very few fields These fields are as follows: ✓ Title: The title of the task as it will show in the list view ✓ Body: The body of the task This is where the user would type in the details ✓ Reminder Date: The date on which the user should be reminded of the task ✓ Reminder Time: The time at which the user should be reminded on the reminder date When complete and running on a device or emulator, the screen looks like Figure 9-1 Figure 9-1: The Add/ Edit Task Reminder screen To create this layout, create a layout file in the res/layout directory with an appropriate name — I’m using reminder_edit.xml To create this file, perform the following steps: 217 218 Part III: Creating a Feature-Rich Application Right-click the res/layout directory and choose New➪Android XML File Provide the name in the File field Leave the default type of resource selected — Layout Leave the folder set to res/layout Set the root element to ScrollView Click the Finish button You now need to provide all the view definitions to build the screen that you see in Figure 9-1 To this, type the code shown in Listing 9-3 Listing 9-3: The reminder_edit.xml File