/* TimePickerDemoActivity.java 2012-09-18 File created by Kari Laitinen 2016-03-31 Last modification. This is an example of how the user can set a time by using the standard TimePickerDialog. This program needs the following layout file: activity_time_picker_demo.xml This program does not work in prior 3.0 Android versions. A DatePickerDialog can be used in the same way as TimePickerDialog. See http://developer.android.com/guide/topics/ui/controls/pickers.html */ package time.picker.demo ; import android.app.Activity ; import android.app.TimePickerDialog ; import android.app.Dialog ; import android.app.DialogFragment ; import android.os.Bundle ; import android.widget.TextView ; import android.widget.TimePicker ; import android.view.View ; import java.util.Calendar ; public class TimePickerDemoActivity extends Activity { private TextView text_view_for_time ; private int current_selected_hour ; private int current_selected_minute ; class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener { @Override public Dialog onCreateDialog( Bundle savedInstanceState ) { // Create a new instance of TimePickerDialog and return it // 'true' as the last parameter means that the time // will be shown in 24-hour format. return new TimePickerDialog( getActivity(), this, current_selected_hour, current_selected_minute, true ) ; } // The following method will be called automatically // after the user has made a selection with the TimePicker public void onTimeSet( TimePicker view, int new_selected_hour_of_day, int new_selected_minute ) { current_selected_hour = new_selected_hour_of_day ; current_selected_minute = new_selected_minute ; update_text_view() ; } } protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState ) ; setContentView( R.layout.activity_time_picker_demo ) ; text_view_for_time = (TextView) findViewById( R.id.text_view_for_time ); // We'll use the current system time as the time that will // be shown to the user in the beginning. Calendar current_system_time = Calendar.getInstance(); current_selected_hour = current_system_time.get( Calendar.HOUR_OF_DAY ) ; current_selected_minute = current_system_time.get( Calendar.MINUTE ) ; update_text_view() ; } // The following method will be called after the user presses // a button on the screen. public void show_time_picker_dialog( View clicked_button ) { DialogFragment time_picker_fragment = new TimePickerFragment() ; time_picker_fragment.show( getFragmentManager(), "timePicker" ) ; } private void update_text_view() { text_view_for_time.setText( String.format( "%02d:%02d", current_selected_hour, current_selected_minute ) ) ; } } /* NOTES This program is a simplified verions of standard Android example DateWidgets1.java. Here is an almost original version of the program: public class TimePickerDemoActivity extends Activity { // where we display the selected date and time private TextView text_view_for_time; // date and time private int mYear; private int mMonth; private int mDay; private int mHour; private int mMinute; static final int TIME_DIALOG_ID = 0; static final int DATE_DIALOG_ID = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView( R.layout.time_picker_demo_activity ) ; text_view_for_time = (TextView) findViewById(R.id.dateDisplay); Button pickDate = (Button) findViewById(R.id.pickDate); pickDate.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { showDialog(DATE_DIALOG_ID); } }); Button pickTime = (Button) findViewById(R.id.pickTime); pickTime.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { showDialog(TIME_DIALOG_ID); } }); final Calendar c = Calendar.getInstance(); mYear = c.get(Calendar.YEAR); mMonth = c.get(Calendar.MONTH); mDay = c.get(Calendar.DAY_OF_MONTH); mHour = c.get(Calendar.HOUR_OF_DAY); mMinute = c.get(Calendar.MINUTE); update_text_view(); } @Override protected Dialog onCreateDialog(int id) { switch (id) { case TIME_DIALOG_ID: return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute, false); case DATE_DIALOG_ID: return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay); } return null; } @Override protected void onPrepareDialog(int id, Dialog dialog) { switch (id) { case TIME_DIALOG_ID: ((TimePickerDialog) dialog).updateTime(mHour, mMinute); break; case DATE_DIALOG_ID: ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay); break; } } private void update_text_view() { text_view_for_time.setText( new StringBuilder() // Month is 0 based so add 1 .append(mMonth + 1).append("-") .append(mDay).append("-") .append(mYear).append(" ") .append(pad(mHour)).append(":") .append(pad(mMinute))); } private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() { public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { mYear = year; mMonth = monthOfYear; mDay = dayOfMonth; update_text_view(); } }; private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() { public void onTimeSet(TimePicker view, int hourOfDay, int minute) { mHour = hourOfDay; mMinute = minute; update_text_view(); } }; private static String pad(int c) { if (c >= 10) return String.valueOf(c); else return "0" + String.valueOf(c); } } ***/