0

I have a time picker dialogue in my settings screen. When user clicks on the time a time picker dialogue appears and user select the time. But when user clicks on the time picker again the time picker dialogue is showing the current time instead of the selected time. Here is what i have done so far.

 mreminder.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Calendar mcurrentTime = Calendar.getInstance();
            final int[] hour = {mcurrentTime.get(Calendar.HOUR_OF_DAY)};
            int minute = mcurrentTime.get(Calendar.MINUTE);
            TimePickerDialog mTimePicker;
            mTimePicker = new TimePickerDialog(SettingsActivity.this, new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
                    int hour = selectedHour;
                    int minutes = selectedMinute;
                    String timeSet = " ";
                    if (hour > 12) {
                        hour -= 12;
                        timeSet = "PM";
                    } else if (hour == 0) {
                        hour += 12;
                        timeSet = "AM";
                    } else if (hour == 12)
                        timeSet = "PM";
                    else
                        timeSet = "AM";

                    String min = "";
                    if (minutes < 10)
                        min = "0" + minutes;
                    else
                        min = String.valueOf(minutes);

                    // Append in a StringBuilder
                    String aTime = new StringBuilder().append(hour).append(':')
                            .append(min).append(" ").append(timeSet).toString();
                    mreminder.setText(aTime);
                    pref.savePref(REMINDER, aTime);
                }
            }, hour[0], minute, false);
            mTimePicker.setTitle("Select Time");
            mTimePicker.show();

        }
    });

Can someone tell me how can i set the timepicker to the selected time when next time user clicks on it.

  • 1
    Possible duplicate of [Android: Setting time in time picker with the time shown in text view](http://stackoverflow.com/questions/12494074/android-setting-time-in-time-picker-with-the-time-shown-in-text-view) – Muhammed Refaat Apr 26 '16 at 09:29
  • Possible duplicate of [How to set custom Time in TimePicker](http://stackoverflow.com/questions/31376072/how-to-set-custom-time-in-timepicker) – T D Nguyen Apr 26 '16 at 09:36
  • I am not able to get it can you please tell me with respect to my code. – Gaurav Tiwari Apr 26 '16 at 09:47

1 Answers1

0

Store the selected time in sharedPreferences and on onClick event check whether the sharedPreference has something. if it is having something then load that data from there you can do something like this:

int hours,mins;//global variables;    
mreminder.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        int h = Integer.parseInt(sharedPreference.getString("HOUR","0));
        int m = Integer.parseInt(sharedPreference.getString("MIN","0"));//"0" is the default value if nothing is stored;
        if(h>0){
               hours = h;
               mins = m;
        }else{
           Calendar mcurrentTime = Calendar.getInstance();
        hours = mcurrentTime.get(Calendar.HOUR_OF_DAY);
        mins = mcurrentTime.get(Calendar.MINUTE);
        }

        TimePickerDialog mTimePicker;
        mTimePicker = new TimePickerDialog(SettingsActivity.this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
                int hour = selectedHour;
                int minutes = selectedMinute;
                String timeSet = " ";
                if (hour > 12) {
                    hour -= 12;
                    timeSet = "PM";
                } else if (hour == 0) {
                    hour += 12;
                    timeSet = "AM";
                } else if (hour == 12)
                    timeSet = "PM";
                else
                    timeSet = "AM";

                String min = "";
                if (minutes < 10)
                    min = "0" + minutes;
                else
                    min = String.valueOf(minutes);

                // Append in a StringBuilder
                String aTime = new StringBuilder().append(hour).append(':')
                        .append(min).append(" ").append(timeSet).toString();
                mreminder.setText(aTime);
                pref.savePref(REMINDER, aTime);
            }
        }, hours, mins, false);
        mTimePicker.setTitle("Select Time");
        mTimePicker.show();

    }
});

This will solve your problem.

Ashwani
  • 1,294
  • 1
  • 11
  • 24
  • please provide the code for sharedprefrences...so that i can help you – Ashwani Apr 26 '16 at 10:58
  • I am saving the preference in the REMINDER. In the above code – Gaurav Tiwari Apr 26 '16 at 11:04
  • can you provide the screen shot of your output....I am not able to get what is the syntax in which you are saving your time..please provide the screenshot – Ashwani Apr 26 '16 at 11:06
  • here is my getPref method: public static T getPref(String key, T defValue) { T returnValue = (T) sharedPreferences.getAll().get(key); return returnValue == null ? defValue : returnValue; } – Gaurav Tiwari Apr 26 '16 at 11:38
  • here i am saving my date : // Append in a StringBuilder String aTime = new StringBuilder().append(hour).append(':') .append(min).append(" ").append(timeSet).toString(); mreminder.setText(aTime); pref.savePref(REMINDER, aTime); – Gaurav Tiwari Apr 26 '16 at 11:38
  • ok the manner in which you are saving your REMINDER is different from what i am storing....for your code do this: Before `Parsing that i have done in my code do this` `String time[] = pref.getPref(REMINDER).split(" "); Sting real_time[] = time[0].split(":");` now do this: `h = Integer.parseInt(time[0].toString()); \\for hour and m = Integer.parseInt(time[1].toString()); \\ for minutes` – Ashwani Apr 26 '16 at 12:21