Search code examples
androidandroid-timepicker

Trouble getting time out of TimePicker


I am trying to get the set time form a time picker from for some reason , it is not happening.

I am using getCurrentHour and getCurrentMinute() but they don't seem to be doing the job.I have tried to clearFocus() but that did not make a difference.

I am trying to get the time on a button click. I have attached the relevant code below , let me know if you need to see anymore.

  Button next_button = (Button) findViewById(R.id.next_button);
    next_button.setOnClickListener(new View.OnClickListener() {
       public void onClick(View arg0) { 

           //Calendar current = Calendar.getInstance();
           Calendar cal = Calendar.getInstance(); 

           //time_picker = (TimePicker) findViewById(R.id.time);

          // time_picker.clearFocus(); // this did not help 
            cal.set(
              time_picker.getCurrentHour(), 
              time_picker.getCurrentMinute(), 
              00);

            Toast.makeText(getApplicationContext(), 
                  "set time: "+cal.getTime(), 
                  Toast.LENGTH_LONG).show(); // this is returning the current time
                                                  // that gets initialised with 
             setAlarm(cal);

       }

    });

my question is : how do i get the the time form the TImePicker?

Thanks for taking the time to read this and for any help that you can give.


Solution

  • cal.set(time_picker.getCurrentHour(), time_picker.getCurrentMinute(), 00);
    

    Calls an overloaded method that is used to set the year, month and day.

    Use the double argument set which is used to set a specified field of your Calendar.

    cal.set (Calendar.HOUR_OF_DAY, time_picker.getCurrentHour());
    cal.set (Calendar.MINUTE, time_picker.getCurrentMinute());
    cal.set (Calendar.SECOND, 0);
    cal.set (Calendar.MILLISECOND,0);