⏰ TimeRangePicker

Jun 8, 2024 | Programming



A customizable, easy-to-use, and functional circular time range picker library for Android. Use this library to mimic Apple’s iOS or Samsung’s bedtime picker.

By Joery Droppers

Screenshots

Playground App

Download the playground app from Google Play, with this app you can try out all features and even generate XML with your selected configuration.

Getting Started

This library is available on Maven Central. To include it in your project, add the following dependency to your build.gradle:

implementation 'nl.joery.timerangepicker:timerangepicker:1.0.0'

Define the TimeRangePicker in your XML layout with custom attributes. See the Configuration section for more information.

<nl.joery.timerangepicker.TimeRangePicker
    android:id="@+id/picker"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    app:trp_thumbIconEnd="@drawable/ic_alarm"
    app:trp_thumbIconStart="@drawable/ic_moon"
    app:trp_endTime="6:30"
    app:trp_startTime="22:00" />

Getting Notified of Time Changes

Listen for changes when the time or duration is adjusted. Here’s how:

picker.setOnTimeChangeListener(object : TimeRangePicker.OnTimeChangeListener {
    override fun onStartTimeChange(startTime: TimeRangePicker.Time) {
        Log.d("TimeRangePicker", "Start time: $startTime")
    }
    override fun onEndTimeChange(endTime: TimeRangePicker.Time) {
        Log.d("TimeRangePicker", "End time: ${endTime.hour}")
    }
    override fun onDurationChange(duration: TimeRangePicker.TimeDuration) {
        Log.d("TimeRangePicker", "Duration: ${duration.hour}")
    }
})

Managing Picker Programmatically

Managing Time

Need to adjust the start time programmatically? You can set it just like ordering food at a restaurant:

  • You can either specify your order by telling the waiter the exact dish you want (specific time):
  • picker.startTime = TimeRangePicker.Time(12, 0)
  • Or, you can just point out how long you’ve been waiting (minutes):
  • picker.startTimeMinutes = 320 // This would set the time to 5:20 AM

When you retrieve the time, the library provides a TimeRangePicker.Time object. You can grab the hour, minute, or total minutes at any moment.

Managing Duration

The same goes for duration. It’s similar to measuring the duration of a movie:

  • Request the length of the film:
  • val duration = picker.endTime.totalMinutes - picker.startTime.totalMinutes
  • Extract exact durations as required using the provided methods.

Listening for Drag Events

Want to catch the moment your patron (the user) starts or stops moving the sliders? Use this listener:

picker.setOnDragChangeListener(object : TimeRangePicker.OnDragChangeListener {
    override fun onDragStart(thumb: TimeRangePicker.Thumb): Boolean {
        // Handle the start of the drag action
        return true  // Return true to allow dragging, false to disallow
    }
    override fun onDragStop(thumb: TimeRangePicker.Thumb) {
        // Handle the end of the drag action
    }
})

Configuration

Customize the appearance of the picker with these attributes:

  • trp_startTime: Set the start time in the “hh:mm” format.
  • trp_endTime: Set the end time in the “hh:mm” format.
  • trp_sliderColor: Choose the color of the slider wheel.
  • … and many more.

Troubleshooting

If you’re facing issues, check for the following common hiccups:

  • Ensure you have the correct dependency in your build.gradle file.
  • Check your XML layout for typos in attribute names.
  • Make sure your Android project is configured to use API 14 or above for compatibility.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Final Thoughts

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox