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.app.Activity;
import ​android.os.Bundle;
import​ android.view.View;
import ​android.widget.Button;
import ​android.widget.TimePicker;
import​ android.widget.Toast;
import android.app.Dialog;
import android.app.TimePickerDialog;

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

​​​​@Override
​​​​public ​void​ onCreate(Bundle​savedInstanceState)​
{
​​​​​​​​super.onCreate(savedInstanceState);
​​​​​​​​setContentView(R.layout.main);
​​​​​​​​showDialog(TIME_DIALOG_ID);
​​​​​​​​timePicker​=​(TimePicker)​findViewById(R.id.timePicker);
​​​​​​​​timePicker.setIs24HourView(true);
​​​​​​​​//---Button​view--- ​​​​​​​​
Button​ btnOpen​ =​ (Button)​ findViewById(R.id.btnSet);
​​​​​​​​btnOpen.setOnClickListener(new​View.OnClickListener()​
{
​​​​​​​​​​​​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:
​​​​​​​​showDialog(TIME_DIALOG_ID);

When the showDialog() method is called, the onCreateDialog() method will be called:
​​​
​@Override
​​​​protected ​Dialog​ onCreateDialog(int ​id) ​​​
​{
​​​​​​​​switch​(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