Android Timepicker with selected time

1k views Asked by At

I am opening time picker on a textview click. The textview is set with the value 12:0:0. But when the time picker is opened, it shows the current time. I want it show 12:0:0 and then let the user change it. How shall I achieve it? Here my code:

    mcurrentTime = Calendar.getInstance();

               int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
               int minute = mcurrentTime.get(Calendar.MINUTE);
               TimePickerDialog mTimePicker;
               mTimePicker = new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener() {
                   @Override
                   public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
                    timePicker.setIs24HourView(true);

                    if(db.getAlarm1().contains(selectedHour + " : " + selectedMinute+" : 0")){
                        alarm1.setText( selectedHour + " : " +( selectedMinute+1)+" : 0"); 
                    }
                    else{
                      alarm1.setText( selectedHour + " : " + selectedMinute+" : 0");  
                    }
                       mcurrentTime.set(Calendar.HOUR_OF_DAY,selectedHour);
                       mcurrentTime.set(Calendar.MINUTE,selectedMinute);
                       mcurrentTime.set(Calendar.SECOND,0);
                     l= mcurrentTime.getTimeInMillis();
                    // alarm1.setText(String.valueOf(l)+" ../.. "+String.valueOf(Calendar.getInstance().getTimeInMillis()));

                   }
               }, hour, minute, true);//Yes 24 hour tim
               mTimePicker.setTitle("Select Time");
               mTimePicker.show();
2

There are 2 answers

0
Chandrakanth On

In your TimePickerDialog constructor in place of hour, minute pass the time which you want to set when it is shown.

0
zsolt.kocsi On

You are injecting the same hour and minute variables into the fragment that you get from your Calendar instance. So instead try setting them manually:

int hour = 12; 
int minute = 0;