Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Android Task Reminder: How to Design the Task Reminder Application

In this article we will introduce the Task Reminder Application, which will be built end to end over the next couple of sections.

Build Android applications is fun but add real world functionality is sometimes critical.The Task Reminder application allows the users to create a list of items with a reminder time associated with each individual task.

The Android platform delivers mechanisms to bring our activity to the foreground when the alarm fires, but that is not an ideal 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, they most likely would be disorganized because an activity started that they did not initiate manually. Therefore, we have various ways in which we can grab the user attention without stealing the main focus away from his current activity.

These mechanisms include the following:

Sl

Mechanism

Application

1

Toasts

Actually the toast is a small view that contains a quick message for the user . This message does not persist since it is available for only a few seconds at most. The toast never collects focus. We will not use a toast for reminding the user. Instead ,we use a toast to notify the user when user activity has been saved, so that the user knows something happened.

2

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 that is located at the top of the screen. The notification items can contain various views and are identified by the icons that we provide. The user can slide the screen down to view the notification.

3

Dialog boxes

Actually the final not-so-popular method to grab a user’s attention is to open a dialog window that can instantaneously steal focus from the user’s presently running app and direct it to a dialog window. While this may indeed work as a method for grabbing the user’s attention, the user may get irritated since their app is stealing focus (possibly on a constant basis if the user has a lot of reminders) from his current actions in another application. We will be using the NotificationManager class to handle the alarms for the Task Reminder application.

Table 1: Showing different mechanisms for messaging.

Process to create 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 of all the current tasks in the application by their name. This view also allows us to delete the task by long-pressing the item. The second view allows to view (Read), add (Create), or edit (Update) a task. Each of the screens eventually interacts with a database for changes to be persisted over the long-term use of the application.

Steps and concept of starting the new project:

To get started, open the Eclipse IDE and create a new Android project with a Build Target of Android 2.2 version and a MinSDKVersion of 4. Provide it with a valid name, package, and activity. The settings we have chosen are shown below. We may also choose to open the illustration Android project. Now we give the system of the new project settings.

#No

Property

Value

1

Project Name

Task Reminder

2

Build Target

Android 2.2 (Application Programming Interface Level 8)

3

Application Name

Task Reminder

4

Package Name

com.sgc.android.taskreminder

5

Create Activity

ReminderListActivity

6

Min Software Development Kit Version

4

Table 2: Showing new project settings

Note that the ‘Create Activity’ property value is ReminderListActivity. Normally we give the first activity in an application the name of the MainActivity, nevertheless the first screen that the user will see is a list of all the current tasks. After that this activity is actually an instance of a ListActivity; hence the name ReminderListActivity.

Process to create the task list:

Actually when we are working with ListActivity classes, we will like to have our layout file. This makes it easy to find when we open the res/layout directory. We are going to rename the main.xml file located in the res/layout directory to reminder_list.xml. After that we just open Eclipse-Integrated Development Environment to rename the file. In Eclipse this process can be done in two ways, we can either right-click the file and choose Refactor -> Rename or select the file and press Shift+Alt+R.

After we change the filename we need to update the name of the file in the setContentView () that is called inside the ReminderListActivity.java file. Now we open the file and change the reference to the new filename we chose. The ReminderListActivity class also needs to inherit from the ListActivity class instead of the regular base activity. Make those changes as well. Our new ReminderListActivity class looks like in the following Listing.

Now a catalog 1 is displayed here:

Listing 1: The ReminderListActivity Class (Catalogue 1)

  public class ReminderListActivity extends ListActivity 
  {
  /*It is Called when the activity is first created in the system. */
  @Override
  public void onCreate(Bundle savedInstanceState) 
  {
  super.onCreate(savedInstanceState); 
  // Super is such types of function that pass data derive class to base class constructor.
  setContentView(R.layout.reminder_list);
  }
  }
  

Our ReminderListActivity references the reminder_list layout resource that currently contains the default code that was easily generated when we created the project. To work with a ListActivity, we need to update this layout with new code as shown below.

Listing 2: Sample showing reminder_list.xml (Catalogue 2)

  <?xml version=1.0” encoding=”utf-8”?>
  <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
  android:layout_width=”wrap_content”
  android:layout_height=”wrap_content”>
  <ListView android:id=”@+id/android:list”                                 //Section I
  android:layout_width=”filling_parent”
  android:layout_height=”filling_parent”/>
  <Text View android: id=”@+id/android: empty”                         //Section II 
  android:layout_width=”wraping_content”
  android:layout_height=”wraping_content”
  android:text=”@string/no_reminders”/>                                    
  </LinearLayout>
  

Now we give a brief explanation of the above code in the following sections:

Section I: In this first section, it defines a ListView, which is an Android view used to show a list of vertically scrolling items. The ID (Identification) of the ListView must be @id/android:list or @+id/android:list.

Section II: In this second section, it 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 the information. This view must have an ID of @id/android:empty or @+id/ android:empty.

Process to create the adding/editing layout:

The layout for accumulation and editing is fairly simple because the form contains very few fields. These fields are as shown as follows:

# No

Component

Requirement

1

Title

This component is the title of the task as it will show in the list view

2

Body

The body of the task. This is where the user would type in the details

3

Reminder Date

This component is used for the date on which the user should be reminded for the task.

4

Reminder Time

The time at which the user should be reminded on the reminder date.

When it is complete and running on a device or emulator, the screen looks like the following Figure 1.

Table 3: Component descriptions

Figure 1: Sample screen

Here we create this layout file in the res/layout directory with an appropriate name, and we are using reminder_edit.xml. Following steps are performed to create the file:

The following steps are given below:

Right Click the res/Layout directory and choose New->Android Extensible Markup Language 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.


Now we need to provide all the view definitions to build the screen that we see above.

Listing 3: The reminder_edit.xml File (Catalogue 3)

  <?xml version=”1.0” encoding=”utf-8”?>
  <ScrollView
  xmlns:android=”http://schemas.android.com/apk/res/android”
  android:layout_width=”fill_parent”
  android:layout_height=”fill_parent”>     //Section I
  <LinearLayout                                                           //Section II
  android:orientation=”vertical”                 //Section III
  android:layout_width=”fill_parent”
  android:layout_height=”fill_parent”>
  <TextView android:layout_width=”wrap_content”
  android:layout_height=”wrap_content”
  android:text=”@string/title” />                 //Section IV 
  <EditText android:id=”@+id/title”
  android:layout_width=”fill_parent”
  android:layout_height=”wrap_content” />                     //Section V
  <TextView android:layout_width=”wrap_content”
  android:layout_height=”wrap_content”
  android:text=”@string/body” />                //Section VI
  <EditText android:id=”@+id/body”
  android:layout_width=”fill_parent”
  android:layout_height=”wrap_content”
  android:minLines=”5”
  android:scrollbars=”vertical”
  android:gravity=”top” />                             //Section VII
  <TextView android:layout_width=”wrap_content”
  android:layout_height=”wrap_content”
  android:text=”@string/date” />                //Section VIII
  <Button
  android:id=”@+id/reminder_date”
  android:layout_height=”wrap_content”
  android:layout_width=”wrap_content”/>           //Section IX
  <TextView android:layout_width=”wrap_content”
  android:layout_height=”wrap_content”
  android:text=”@string/time” />                //Section X
  <Button
  android:id=”@+id/reminder_time”
  android:layout_height=”wrap_content”
  android:layout_width=”wrap_content” />  //Section XI
  <Button android:id=”@+id/confirm”
  android:text=”@string/confirm”
  android:layout_width=”wrap_content”
  android:layout_height=”wrap_content” />  //Section XII
  </LinearLayout>
  </ScrollView>
  

This program is very useful for developing the Task Reminder Application of Android. After writing the code we give a brief explanation of the program with section categories as per mentioned above. Please check the explanation below:

  • Section I: The parent view is a ScrollView, which creates a scroll bar and allows the view to be scrolled when the contents of the view are too big for the screen in the device. Now we give the screen shown in Figure 2 and is shown in portrait mode. However, if the device is rotated 90 degrees, the view flips and over half of the view is automatically cut off. The parent ScrollView allows the remaining contents of the screen to be scrollable. Therefore, the user can move his finger upward on the screen to scroll the contents up and see the remainder of the view.
  • Section II: In this section we are discussing that a ScrollView can only have one child. In this particular case it is the main LinearLayout that houses the rest of the layout.
  • Section III: In this section we are covering the orientation of the linear layout set to vertical to signify that the views inside this layout should be stacked on top of one another.
  • Section IV: In this section we give the label for the Title field also.
  • Section V: In this section we can EditText that allows the user to provide a title for the task.
  • Section VI: In this section we just give label for the Body field as per design.
  • Section VII: In this section we create the EditText that defines the Body field. The EditText view has set the minLines property to 5(five) and the gravity property to the top. This informs the Android platform that the EditText is at least five lines tall, and when the user starts typing, the text should be bound to the top of the view with the gravity process.
  • Section VIII: In this section we set up the reminder date label. This label also uses a string resource. Now we will need to add a string resource with the name of “date” and a value of “Reminder Date”.
  • Section IX: In this section we also work with the reminder date button. When this button is clicked then we find out that a DatePickerDialog is automatically launched and this allows the user to choose the date with a built-in Android date picker. When the date is set via the DatePicker then the value of the date is set as the button text.
  • Section X: In this section we set the reminder time label control and this label uses a string resource. We will need to add a string resource with the name of “time” and a value of “Time”.
  • Section XI: In this section we set the time reminder button. When this button is clicked and the TimePicker is automatically launched and it also allows the user to choose a time with a built-in Android time picker. When the time is set via the TimePickerDialog, the value of the time is set as the button text.


Figure 2: The Task Reminder running with fake/ stubbed data

Process to handle user click events in the Android Task Reminder application:

The items in the list are click events that allow the user to interact with each item. The Android View objects have two main types of click events:

Sl.No

Event

Application

1

Click

This event is used by the user to tap a view such as a button.

2

Long click

This Long Click is used by the user to tap and hold their finger on a button for a few moments. Each view and/or activity can intercept these events via various types of methods. In the following section we show the user how to respond to each type of event in a ListActivity. We will also try to demonstrate responding to a Button click events action.

3

Short clicks


The ListActivity in Android does a lot of event-handling heavy lifting for the user.

Table 4: Click events given shown

After the onCreate() method now we type another method:

  @Override
  protected void onListItemsClick(ListView list, View view, int position, long identy) 
  {
  super.onListItemsClick(list, view, position, identity);
  //Super is use for send the data derive to base class constructor.
  }
  

This code overrides the default implementation of onListItemsClick(), that is provided by the ListActivity. At this stage we generate the list item that is clicked and this method is called and the following parameters are passed into the callview level:

Sl

Call View

Application

1

l:

The ListView where the click happened

2

v:

The item that was clicked with the ListView

3

position:

It checks the position of the clicked item in the list.

4

id:

It identifies the row ID of the item that was clicked

Table 5: Call view parameter lists

Using these above variables we can determine which item was clicked and then perform an action based on that information. When an item is clicked in this list we will be starting an intent that opens the ReminderEditActivity to allow us to edit the item as shown in the section “Step of starting new activities with the intents”.

Long clicks:

These Long clicks also known as long presses occur when a user presses a view for an extended period of time. To handle the list of the items long click event in a ListActivity process and add the subsequent line of code at the end of the onCreate() method.

  registerForContextMenu(getListView());
  

This outer method shown above registerForContextMenu () is responsible for registering a context menu to be shown for a given view or multiple views. Context menu is not just limited to a single view process. It means that each list item is eligible to create a context menu item and the registerForContextMenu () accepts a View object as a parameter. And the ListActivity should register as eligible for the context menu creation process. The inner method like getListView () returns a ListView object that is used for the registration. The call getListView () is a member of the ListActivity class.

Now that we have registered the ListView to be eligible to create a context menu we need to respond to the long-click event on any given item. When an item is long-clicked in the ListView, the registerForContextMenu () recognizes this and calls the onCreateContextMenu () method when the context menu is ready to be created. In this method we can set up our context menu.

At the end of the class file type the following method:

  @Override
  public void onCreateContextMenu(ContextMenu cmenu, View ve, ContextMenuInfo menuInfo) 
  {
  super.onCreateContextMenu(cmenu, ve, menuInfo);
  }
  


This method is called with the following parameters as listed below

Sl

Call View Parameter

Application

1

cmenu

This parameter is Actually the context menu that is being built up.

2

ve

This parameter is the view for which the context is being built up (the view that we long-clicked on).

3

menuInfo:

This parameter is used for Extra information about the item for which the context menu should be shown. This can vary depending on the type of view in the ve parameter.

Table 6: Parameter lists

Inside this method we can modify the menu that will be presented to the user. As per illustration when the user long-presses an item in the task list we want to allow her to delete it. Therefore we need to present them with a Delete context menu option.

Process for identifying our intent:

Though some applications have only two screens as the Task Reminder application, but a lot is happening behind the scenes. One such notable interaction that happens between the application and the user is the introduction of new screens as the user utilizes various features of the application. As with any type of application with a rich feature, set the user to interact with each screen independently. Consequently the big question automatically arises: “How do we open another screen”? As it is very common requirement to communicate with more than one page. The Screen interaction is handled through the Android’s intent system. Unfortunely we have not covered an illustration of how to navigate from one screen to the next using an intent process.

Step of starting new activities with the Intents:

The Activities are initiated through the Android intent framework (We all known that the Framework is actually a Template of Software Development platform). Intent is a class that represents a message that is placed on the Android intent system that is also similar to a message-bus type of architecture model and it can respond to the intent. One of the best ways to describe intent is to think of it as an abstract description of an operation. Starting a particular activity is very easy. In our ReminderListActivity type the following code into the onListItemClick () method:

  @Override
  protected void onListItemClick(ListView list, View ve, int position, long id) 
  {
  super.onListItemClick(list, ve, position, id); // Formal Parameter
  Intent i = new Intent(this, ReminderEditActivity.class);   //Section I 
  i.putExtra(“RowId”, id);  //Section II
  startActivity(i);    //Section III
  }
  

Here we give a brief explanation of each line in three sections:

  • Section I: In the first section we are creating a new intent by using the Intent constructor that accepts the current context which is this (the current running activity), as well as a class that the Intent system should attempt to start as the Reminder Edit activity process.
  • Section II: In the second section we place some extra data into the Intent object. In this instance we are placing a key/value pair into the intent. The key is RowId, and the value is the ID (Identification) of the view that was clicked here. This value is placed into the intent so that the receiving activity (the ReminderEditActivity) can pull this data from the Intent object and use it to load the information about the intent process. Now we are providing fake/stub data; therefore have nothing displayed.
  • Section III: In the third section we start the activity from within the current activity. This calling places the intent message onto the Android intent system and it also allows Android to decide in what manner to open that screen for the user.

Process to retrieve values from previous activities:

Sometimes we need to be able to pull the data out of the incoming intent to figure out what to do with it. As demonstrated in the section “Steps for starting new activities with intents” in the earlier section of this article, we also provided some extra data and information with the intent. This is the RowId and later we will use this RowId on the ReminderEditActivity to pull the data from the SQLite (Part of the MySql Server) database and display it to the user.

To pull the data out of an incoming intent, type the following at the end of the destination activity’s onCreate() method which would be the ReminderEditActivity:

  if(getIntent() != null) {     //Section I 
  Bundle extras = getIntent().getExtras();   //Section II
  int rowId = extras != null ? extras.getInt(“RowId”) : -1;   //Section III
  // Do stuff with the row id here
  }
  

Brief explanation of each section of code is described below:

  • Section I: In the first section the getIntent() method is provided by the Activity base class. This method retrieves any incoming intent to the activity. On this section we are making sure that it is not null so that we also know it is safe to work with it.
  • Section II: In the second section the bundle is retrieved from the intent via the getExtras() call. A bundle is a simple key/value pair with the data structure.
  • Section III: In the third section we are using the ternary operator to identify whether the bundle is null. If the bundle is not null (give some value) we can retrieve the RowId that is contained in the intent that was sent from the previous activity through the getInt() method. Although we are not doing anything with it in this instance, we use this row ID to query the SQLite database to retrieve the Task record to edit. When the SQLite database is in place (which is also a part of the MySql Server), the record will be retrieved from the database and the various values of the task will be presented to the user on the screen via an editable form so that the user can edit the task very easily.

The Task Reminder Application is one of most important part in the Android programming. Through this process, user can access different devices and this access procedure is very smooth and well defined. In the above discussion, we have seen different level of program and job that the user can easily understand and implement. Actually, Android is such types of device that can have many types of applications and we are also not aware of all types of usage. But in this article we have explained concepts and implementation details, so that the user can very easily access the device. We have also provided suitable code in sections and these sections are also explained step by step for better understanding.



Website: www.techalpine.com Have 16 years of experience as a technical architect and software consultant in enterprise application and product development. Have interest in new technology and innovation area along with technical...

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login