Android time picker example
In this example, the time picker dialog TimePickerDialog will popup when the EditText is clicked. After the user picks a time and hit ok, the time will show up on the EditText. The EditText is only clickable and not focusable.
The EditText in a layout file
The code for the TimePickerDialog in an Activity class
//private field variables in the Activity class
private Calendar myCalendar = Calendar.getInstance();
private EditText etTime;
//onCreate method of the Activity class
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etTime = (EditText) findViewById(R.id.et_time);
etTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int hour = myCalendar.get(Calendar.HOUR_OF_DAY);
int minute = myCalendar.get(Calendar.MINUTE);
TimePickerDialog timePickerDialog;
timePickerDialog = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
etTime.setText( selectedHour + ":" + selectedMinute);
}
}, hour, minute, true); // true means set 24hoursview to true
timePickerDialog.setTitle("Select Time");
timePickerDialog.show();
}
});
}
Search within Codexpedia
Custom Search
Search the entire web
Custom Search
Related Posts