How can i save the hourOfDay and minutes from timePickerDialog to compare it with some other variable?

281 Views Asked by At

I want to get hourOfDay and minutes from TimePickerDialog inputted by the user but the values are shown zero out of the onTimeSet method.

       LoadTime.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            calendar = Calendar.getInstance();
            currentHour = calendar.get(Calendar.HOUR_OF_DAY);
            currentMinute = calendar.get(Calendar.MINUTE);
            timePickerDialog = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker timePicker, int hourOfDay, int minutes) {
                    if (hourOfDay >= 12) {
                        amPm = "PM";
                    } else {
                        amPm = "AM";
                    }
                    LoadTime.setText(String.format("%02d:%02d", hourOfDay, minutes) + amPm);
                }
            }, currentHour, currentMinute, false);
            Log.d("INT","val: " + hour);
            timePickerDialog.show();
        }
    });

I want to save these two variables and perform some if-else conditioning. Thanks in Advance.

2

There are 2 best solutions below

2
pecific_rim On

you are taking the log outside onTimeSet() method.

LoadTime.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        calendar = Calendar.getInstance();
        currentHour = calendar.get(Calendar.HOUR_OF_DAY);
        currentMinute = calendar.get(Calendar.MINUTE);
        timePickerDialog = new TimePickerDialog(MainActivity.this, new 
        TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker timePicker, int hourOfDay, int minutes) {
                if (hourOfDay >= 12) {
                    amPm = "PM";
                } else {
                    amPm = "AM";
                }
                 Log.d("INT","val: " + hourOfDay);
                LoadTime.setText(String.format("%02d:%02d", hourOfDay, minutes) + amPm);
            }
        }, currentHour, currentMinute, false);
        timePickerDialog.show();
    }
});`

To store the value in string, java string .valueOf() method converts different types of values into string. e.g:

String hourOfDayInString = String.valueOf(hourOfDay);
6
Gaket On

If I get you right, you want to get the hour of day and minutes.

If you get the correct values in the callback, there are 3 main reasons why they maybe incorrect outside of it:

  1. You are rewriting that values in some other place
  2. You have variables with the same name in another scope, so they are overloaded
  3. You are querying the numbers before a user actually selects the data

You can check out a working example here: https://mobikul.com/select-time-using-time-picker-dialog/

And if we go from your example, have you tried to use some class fields to store the numbers? Like this:

class YourClass {

// Create you variables as class fields
int hourOfDay;
int minutes;

void yourInitMethod() {
    LoadTime.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            calendar = Calendar.getInstance();
            currentHour = calendar.get(Calendar.HOUR_OF_DAY);
            currentMinute = calendar.get(Calendar.MINUTE);
            timePickerDialog = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker timePicker, int hourOfDay, int minutes) {

                    // assign field variables
                    this.hourOfDay = hourOfDay;
                    this.minutes = minutes;

                    if (hourOfDay >= 12) {
                        amPm = "PM";
                    } else {
                        amPm = "AM";
                    }
                    LoadTime.setText(String.format("%02d:%02d", hourOfDay, minutes) + amPm);
                }
            }, currentHour, currentMinute, false);

            // This one will show you the hour
            Log.d("INT","val: " + String.valueOf(hourOfDay));
            timePickerDialog.show();
        }
    });
}