Monday, 2 March 2015

Android TimePicker Dialog

Displaying the TimePicker in a Dialog Window 

While you can display the TimePicker in an activity, a better way is to display it in a dialog window, so that once the time is set, it disappears and doesn’t take up any space in an activity.

Using a Dialog to Display the TimePicker View 

1 .

import ​android.os.Bundle;
import​ android.view.View;
import ​android.widget.Button;
import ​android.widget.TimePicker;
import​ android.widget.Toast;

public​ class ​MainActivity ​extends​ Activity
​​​​int hour, minute;
​​​​static final int TIME_DIALOG_ID = 0;
/**​Called​ when​ the ​activity ​is ​first ​created.​*/

​​​​public ​void​ onCreate(Bundle​savedInstanceState)​
​​​​​​​​//---Button​view--- ​​​​​​​​
Button​ btnOpen​ =​ (Button)​ findViewById(;
​​​​​​​​​​​​public ​void ​onClick(View​ v)
​{ ​
​​​​​​​​​​​​​​​Toast.makeText(getBaseContext(), ​​​​​​​​​​​​​​​​​​​​​​​​“Time​selected:”​+ ​​​​​​​​​​​​​​​​​​​​​​​​​​​timePicker.getCurrentHour().toString()​+​“:”​+​timePicker.getCurrentMinute().toString(), ​​​​​​​​​​​​​​​​​​​​​​​​Toast.LENGTH_SHORT).show(); ​​​​​​​​​​​​​​​​
@Override ​
​​​protected Dialog onCreateDialog(int id)
​​​​​​​​switch (id)
​​​​​​​​​​​​case TIME_DIALOG_ID: ​​​​​​​​​​​​​​​​return new TimePickerDialog( ​​​​​​​​​​​​​​​​​​​​this, mTimeSetListener, hour, minute, false); ​​​​​​​​} ​​​​​​​​
return null;
private TimePickerDialog.OnTimeSetListener mTimeSetListener = ​​​​​​​​new TimePickerDialog.OnTimeSetListener() ​​​​​​​​{
​​​​​​​​​​​public void onTimeSet( ​​​​​​​​​​​​TimePicker view, int hourOfDay, int minuteOfHour) ​​​​
​​​​​​​​​​​​​​​​hour = hourOfDay; ​​​​
​​​​​​​​​​​​minute = minuteOfHour;
​​​​​​​​​​​​​​​​Toast.makeText(getBaseContext(), ​​​​​​​​​​​​​​​​​​​​“You have selected : “ + hour + “:” + minute, ​​​​​​​​​​​​​​​​​​​​Toast.LENGTH_SHORT).show(); ​​​​​​​​​​​​
2 . When the activity is loaded, you can see the TimePicker displayed in a dialog window. Set a time and then click the Set button. You will see the Toast window displaying the time that you just set.

How It Works 

To display a dialog window, you use the showDialog() method, passing it an ID to identify the source of the dialog:

When the showDialog() method is called, the onCreateDialog() method will be called:
​​​​protected ​Dialog​ onCreateDialog(int ​id) ​​​
​{ ​
​​​​​​​​​​​case​TIME_DIALOG_ID: ​​​​​​​​​​​​​​​​return ​new​ TimePickerDialog( ​​​​​​​​​​​​​​​​​​​​this,​mTimeSetListener,​hour,​minute,​false);
​​​​​​​​return ​null;
Here, you create a new instance of the TimePickerDialog class, passing it the current context, the callback, the initial hour and minute, as well as whether the TimePicker should be displayed in 24-hour format. When the user clicks the Set button in the TimePicker dialog window, the onTimeSet() method will be called:

​​​​private​ TimePickerDialog.OnTimeSetListener ​mTimeSetListener​= ​​​​​​​​new​TimePickerDialog.OnTimeSetListener() ​​​​​​​​
​​​​​​​​​​​​public​ void​ onTimeSet( ​​​​​​​​​​​​TimePicker​ view,​int​ hourOfDay,​int ​minuteOfHour) ​​​
​​​​​​​​​​​​​​​​hour​=​hourOfDay; ​
​​​​​​​​​​​​​​​minute​=​minuteOfHour; ​​​
​​​​​​​​​​​​​Toast.makeText(getBaseContext(), ​​​​​​​​​​​​​​​​​​​​“You​have​selected​:​“​+​hour​+​“:”​+​minute, ​​​​​​​​​​​​​​​​​​​​Toast.LENGTH_SHORT).show();
​​​​​​​​​​​​} ​

Here, the onTimeSet() method will contain the hour and minute set by the user via the hourOfDay and minuteOfHour arguments, respectively.

No comments:

Post a Comment