Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TimePicker SetHour Method

Tags:

android

I obtain a warning in Android Studio upon using the setCurrentHour Method (as below), that it has been deprecated.

timePicker.setCurrentHour(hour);

Meanwhile, upon using the setHour Method (as hereafter), I obtain a warning stating that the "Call requires API level 23" while the current minimum is lower.

timePicker.setHour(hour);

Is there a Support Library that I could use for Backward Compatibility, avoiding thus any warnings?

like image 818
JF0001 Avatar asked May 10 '17 02:05

JF0001


People also ask

What is TimePicker?

android.widget.TimePicker. A widget for selecting the time of day, in either 24-hour or AM/PM mode. For a dialog using this view, see TimePickerDialog .


1 Answers

I'm not sure if there is already a support library for this, but this is what I've done when I was dealing with these methods:

Java:

    private void setTime(int hour, int minute) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
          mTimePicker.setHour(hour);
          mTimePicker.setMinute(minute);
        } else {
          mTimePicker.setCurrentHour(hour);
          mTimePicker.setCurrentMinute(minute);
        }
    }

And in Kotlin:

    private fun setTime(hours: Int, minutes: Int) {
        @Suppress("DEPRECATION")
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            time_picker.hour = hours
            time_picker.minute = minutes
        } else {
            time_picker.currentHour = hours
            time_picker.currentMinute = minutes
        }
    }
like image 174
fmpsagara Avatar answered Sep 24 '22 19:09

fmpsagara