Create an elegantly designed Reminder/Alarm clock application
DownloadKeywords: ListActivity SimpleCursorAdapter SQLiteDatabase AlarmManager NotificationManager IntentService BroadcastReceiver ToggleButton ViewSwitcher DatePicker TimePicker RadioGroup
Contents- Overview
- Create a new Eclipse Android project
- Define the Data model
- The Android Manifest file
- The Application class
- The Preferences screen
- The Alarm Service
- The Alarm Receiver
- The Alarm Setter
- The Main screen
- The Options Menu
- The Context Menu
- The Edit Dialog
- The New Reminder screen
- Date and Time Controls
Here is an excerpt from AddAlarmActivity.java.
public class AddAlarmActivity extends Activity { private ViewSwitcher vs; private RadioGroup rg; private RelativeLayout rl3; private RelativeLayout rl4; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTitle("New Reminder"); setContentView(R.layout.add); vs = (ViewSwitcher) findViewById(R.id.view_switcher); rg = (RadioGroup) findViewById(R.id.radioGroup); rl3 = (RelativeLayout) findViewById(R.id.relativeLayout3); rl4 = (RelativeLayout) findViewById(R.id.relativeLayout4); rg.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch(checkedId) { case R.id.radio0: rl3.setVisibility(View.VISIBLE); rl4.setVisibility(View.GONE); break; case R.id.radio1: rl4.setVisibility(View.VISIBLE); rl3.setVisibility(View.GONE); break; } } }); } public void onClick(View v) { switch (v.getId()) { case R.id.toggleButton: vs.showNext(); break; } } }We had specified a onClick() function on toggleButton in add.xml which is implemented in this activity. It simply calls showNext() on the view switcher and Android takes care of the functionality to switch the views.
We have implemented similar functionality using a RadioGroup and RelativeLayouts for changing between Rule and Interval.
15. Date and Time Controls
Finally, we discuss about the mechanism for selecting date and time. Android provides ready made widgets for this purpose like DatePicker and TimePicker, so it is pretty straight forward to capture the values in code using the following getter functions.datePicker.getYear(); datePicker.getMonth(); datePicker.getDayOfMonth(); timePicker.getCurrentHour(); timePicker.getCurrentMinute();However, in include_repeating.xml we launch the corresponding dialogs programmatically so it requires a little bit of work.
@Override protected Dialog onCreateDialog(final int id) { Calendar cal = Calendar.getInstance(); switch(id) { case DIALOG_ATTIME: TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() { public void onTimeSet(TimePicker view, int hourOfDay, int minute) { // TODO capture time } }; return new TimePickerDialog(this, mTimeSetListener, cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), false); case DIALOG_FROMDATE: case DIALOG_TODATE: DatePickerDialog.OnDateSetListener dateListener = new DatePickerDialog.OnDateSetListener() { public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // TODO capture date } }; return new DatePickerDialog(this, dateListener, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE)); } return super.onCreateDialog(id); } @Override protected void onPrepareDialog(int id, Dialog dialog) { super.onPrepareDialog(id, dialog); switch(id) { case DIALOG_ATTIME: ((TimePickerDialog)dialog).updateTime(hourOfDay, minute); break; case DIALOG_FROMDATE: ((DatePickerDialog)dialog).updateDate(year, monthOfYear, dayOfMonth); break; case DIALOG_TODATE: ((DatePickerDialog)dialog).updateDate(year, monthOfYear, dayOfMonth); break; } }And do update the onclick() function to show proper dialog.
case R.id.fromdate_lb: showDialog(DIALOG_FROMDATE); break; case R.id.todate_lb: showDialog(DIALOG_TODATE); break; case R.id.attime_lb: showDialog(DIALOG_ATTIME); break;
This completes our tutorial. You may want to check out our other tutorials to learn some more tricks and tips to develop awesome Android apps!