1

Is it possible to popup a TimePicker when an EditText is clicked/touched?

I've tried looking for examples of this but I haven't found any. I want to use this time entered to set up an alarm, so it'd be great if someone could tell me how to do this and provide me of an example.

I'd prefer it if the Time was in 24 hour format instead of 12 hour format.

Thanks for any answers submitted.

Mark O'Sullivan
  • 10,138
  • 6
  • 39
  • 60

2 Answers2

2

I will give you some tips to achieve that.

  1. To show popup/dialog after EditText is clicked/touched you will find answer here: Android EditText onClickListener. Also is there discussed if popping dialog after clicking on EditText is standard/non-standard interface.

  2. For TimePicker you can use system TimePickerDialog. You can find tutorial for TimePickerDialog here: http://www.pavanh.com/2013/04/android-timepicker-example.html

  3. To use 24 hour format instead 12 hour format in TimePicker you can use following method: timePicker.setIs24HourView(true), or if you use TimePickerDialog you will pas true in constructor.

Hope it will help you.

Community
  • 1
  • 1
Koso
  • 3,200
  • 2
  • 20
  • 22
  • Is there no way to do it without a DialogFragment? I got the EditText to open up a DatePicker without having to use a complicated custom DialogFragment – Mark O'Sullivan Jul 27 '13 at 19:52
  • Yes, check out this link, I've posted the code I've tried - http://stackoverflow.com/questions/17901946/timepicker-dialog-from-clicking-edittext – Mark O'Sullivan Jul 27 '13 at 20:01
  • I updated point 2. You can use system TimePickerDialog. So there is no need to create custom dialog. I think tutorial from point 2 can help you solve question you posted to comment. – Koso Jul 27 '13 at 20:01
  • Missed one tiny bit of code, got it sorted. +1 for trying to help me out and providing a tutorial for the TimePickerDialog – Mark O'Sullivan Jul 27 '13 at 20:32
1

This is the solution. I forgot about the true/false boolean and that's why it wasn't working when I tried something similar to the DatePicker code.

eReminderTime.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Calendar mcurrentTime = Calendar.getInstance();
                int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
                int minute = mcurrentTime.get(Calendar.MINUTE);
                TimePickerDialog mTimePicker;
                mTimePicker = new TimePickerDialog(AddReminder.this, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
                        eReminderTime.setText( "" + selectedHour + ":" + selectedMinute);
                    }
                }, hour, minute, true);
                mTimePicker.setTitle("Select Time");
                mTimePicker.show();

            }
        });
Mark O'Sullivan
  • 10,138
  • 6
  • 39
  • 60