Lesson 58. Dialogues. TimePickerDialog

Lesson 58. Dialogues. TimePickerDialog

In this lesson:

– we use TimePickerDialog

Let’s start with a new topic – dialogues. Dialog is a small popup that is most often used to confirm any operations or enter a small amount of data.

We will look at standard dialogues provided by the system and learn how to do ours. In this tutorial, we’ll look at TimePickerDialog. This is a dialog that allows you to specify the time.

Let’s make a simple application. The screen will have TextView, it will be clickable and a dialog will appear to select the time. When selected, it will be displayed in TextView.

Let’s create a project:

Project name: P0581_TimePickerDialog
Build Target: Android 2.3.3
Application name: TimePickerDialog
Package name: ru.startandroid.develop.p0581timepickerdialog
Create Activity: MainActivity

screen main.xml:


On the screen TextViewWhich we will include clickable and assign a processing method onclick.

code MainActivity.java:

package ru.startandroid.develop.p0581timepickerdialog;

import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.TimePicker;

public class MainActivity extends Activity {
  int DIALOG_TIME = 1;
  int myHour = 14;
  int myMinute = 35;
  TextView tvTime;
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        tvTime = (TextView) findViewById(R.id.tvTime);
    public void onclick(View view) {
    protected Dialog onCreateDialog(int id) {
      if (id == DIALOG_TIME) {
        TimePickerDialog tpd = new TimePickerDialog(this, myCallBack, myHour, myMinute, true);
        return tpd;
      return super.onCreateDialog(id);
    OnTimeSetListener myCallBack = new OnTimeSetListener() {
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
      myHour = hourOfDay;
      myMinute = minute; 
      tvTime.setText("Time is " + myHour + " hours " + myMinute + " minutes");

IN onCreate we find TextView.

IN onClick call the showDialog method and pass it the dialog ID. This method (showDialog) creates a dialog using a separate method and shows it. The ID is used to specify which dialog to create and display.

method onCreateDialog This is a separate method that is called in showDialog to create a dialog. In this method, we look at what ID came in, create the appropriate dialog and return it.

In our case, we create a TimePickerDialog using the constructor:

TimePickerDialog (Context context, TimePickerDialog.OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView), where

context – context
callBack is a TimePickerDialog.OnTimeSetListener interface handler whose method works when you click OK in the dialog
hourOfDay – an hour that will show the dialogue
minutes – the minute the dialogue will show
is24HourView – 24 hour time format (otherwise AM / PM)

myCallBack is an object that implements the TimePickerDialog.OnTimeSetListener interface. He has only one method – onTimeSet, which gives us a TimePicker from the dialog, and the hour and minute it shows. That is what we have introduced in the dialogue.

We write this data on tvTime.

We’ll save everything and run it. Click on TextView. A dialog for entering the time has appeared.

Now it shows 14:35 because when we created it we passed it the values ​​myHour and myMinute.

change the value

and click Set.

The text showed new meanings.

The dialog can also be closed with a button Cancel or button Back (back) on the emulator. In this case, the handler method does not work and the text is not updated.

This is the simplest dialogue. And here, of course, not all possibilities are considered. Here it is important to understand the schema of showDialog and onCreateDialog, and the presence of a dialog result handler.

We also used the new TimePicker component here. It is simple and easy to set or display the time.

In the next lesson we will make a similar example, but with a date, for fixing.

In the next lesson:

– we use DatePickerDialog

Discuss in the forum [31 replies]

Leave a Comment