1

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.

Emon
  • 63
  • 9

2 Answers2

2

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);
Gourav
  • 2,746
  • 5
  • 28
  • 45
pecific_rim
  • 105
  • 1
  • 10
  • Pardon me, saving the values in the string is not my priority. I have taken log inside onTimeSet and it showed accurate value but I want to perform further operation on these two values so is there any way I can use them out of the scope. – Emon Jan 14 '19 at 16:27
  • make "hourOfDayInString" as class variable and assign value in it onTimeSet() method. You can do the same for minute. – pecific_rim Jan 14 '19 at 16:32
1

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();
        }
    });
}
Gaket
  • 6,533
  • 2
  • 37
  • 67
  • Yes, you are right. I wanted to use hourOfDay and minute in another scope, so I tried to store them in separate variables but it did not work out either. – Emon Jan 14 '19 at 16:16
  • But that's literally impossible. Have you checked all the 3 points that may go wrong that I've posted? Can you show a gist with the whole activity? – Gaket Jan 14 '19 at 16:23
  • Sorry, I did not get you properly but i have added the gist here(https://gist.github.com/Ibrahim-ullah/a735a2883e4aa4ae0938d05854c614bc) – Emon Jan 14 '19 at 16:39
  • you haven't set the variables as in my example. Try it out, please: // assign field variables this.hourOfDay = hourOfDay; this.minutes = minutes; – Gaket Jan 14 '19 at 17:20
  • Moreover, do you understand, that these values will be logged only on the second click anyway? Because now the logging happens earlier than user selects a date. Try to put debug breakpoints there and go step by step. With my example, it will be like this: first, you tap on it and get logs with zeros. When you tap the second time, the first selection will be logged. – Gaket Jan 14 '19 at 17:21
  • I have tried to assign variable just like you did but it was showing error. Though I have solved the problem using `getText()` but thanks a lot for your co-operation. – Emon Jan 15 '19 at 03:12