Code Monkey home page Code Monkey logo

snaptimepicker's Introduction

Android Arsenal Maven Central Minimum SDK Version Workflow Status

Snap Time Picker

Another Material Time Picker for developer who do not like default Material Time Picker that difficult to use for most users

Snap Time Picker Sample

Download

Since version 1.0.3 will move from JCenter to MavenCentral

// build.gradle (project)
allprojects {
    repositories {
        mavenCentral()
        /* ... */
    }
}

Gradle

implementation 'com.akexorcist:snap-time-picker:1.0.3'

Feature

  • iOS Time Picker with Material Design style
  • Some text & color customization
  • Selectable time range support
  • ViewModel support for event callback with LiveData (See example)

Snap Time Picker Sample

Usage

Relevant class in SnapTimePicker

  • SnapTimePickerDialog - Main Class
  • TimeValue - Time data holder that contain hour and minute
  • TimeRange - Time range data holder that contain the range of time with start (TimeValue) and end (TimeValue)

To use the SnapTimePicker you have to create the SnapTimePickerDialog from builder

val dialog = SnapTimePickerDialog.Builder().build()
//
dialog.show(supportFragmentManager, tag)

SnapTimePickerDialog made from DialogFragment (AndroidX) so it need SupportFragmentManager from Activity/Fragment and any string tag. If you have no idea for the dialog tag. You can use SnapTimePickerDialog.TAG

Note - Cannot reuse the SnapTimePickerDialog instance. Please create new instance every time

To custom some text and color in TimePickerDialog.

SnapTimePickerDialog.Builder().apply {
    setTitle(R.string.title)
    setPrefix(R.string.time_suffix)
    setSuffix(R.string.time_prefix)
    setThemeColor(R.color.colorAccent)
    setTitleColor(R.color.colorWhite)
}.build().show(supportFragmentManager, tag)

Title, Prefix and Suffix must be define with string resource. ThemeColor and TitleColor must be color resource

Text Customization

Color Customization

To custom the positive and negative button.

SnapTimePickerDialog.Builder().apply {
    setPositiveButtonText(R.string.accept)
    setNegativeButtonText(R.string.reject)
    setPositiveButtonColor(R.color.white)
    setNegativeButtonColor(R.color.white)
    setButtonTextAllCaps(false)
}.build().show(supportFragmentManager, tag)

Positive and negative button text will be all-capitalized by default.

To set pre-selected time and time range in TimePickerDialog.

SnapTimePickerDialog.Builder().apply {
    setPreselectedTime(TimeValue(2, 34))
    setSelectableTimeRange(TimeRange(TimeValue(2, 15), TimeValue(14, 30)))
}.build().show(supportFragmentManager, tag)

Color Customization

For event callback from SnapTimePicker, you have assign the listener after build the SnapTimePickerDialog from builder.

SnapTimePickerDialog.Builder().apply {
    // 
}.build().apply{
    setListener { hour, minute -> 
        // Do something when user selected the time 
    }
}.show(supportFragmentManager, tag)

But use listener does not good enough if the app can work in portrait and landscape. To support screen orientation, call useViewModel() in SnapTimePickerDialog then observe the event callback from SnapTimePicker's ViewModel from SnapTimePickerUtil

SnapTimePickerDialog.Builder().apply {
    useViewModel()
}.build().show(supportFragmentManager, SnapTimePickerDialog.TAG)

SnapTimePickerUtil.observe(this) { selectedHour: Int, selectedMinute: Int ->
    onTimePicked(selectedHour, selectedMinute)
}

SnapTimePickerDialog can be called from anywhere in your code but SnapTimePickerUtil.observe(...) must called in onCreate() only (That's how ViewModel and LiveData works).

Licence

Copyright 2021 Akexorcist

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with the License. You may obtain a copy of the License in the LICENSE file, or at:

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

snaptimepicker's People

Contributors

adrianhartanto004 avatar akexorcist avatar nitinrajyadav avatar theerasan-salutat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

snaptimepicker's Issues

Time interval

Hi
I am using time range picker and set a time interval of 5 minutes to show time every 5 minutes so 1:00,1:05,1:10 etc
But this does not work like this.Is it supported in this library or not?

incorrect download info

Hi. I had a problem following your instructions to download the time picker to my project. It would be really helpful if you could change download info in your readme from 'com.akexorcist:snaptimepicker:1.0.0' to 'com.akexorcist:snap-time-picker:1.0.0'. Thank you.

image

FlingListener already set

Hi thanks for your library, I'm using it on Java. If I open the dialog, set the time and reopen it I get:

java.lang.IllegalStateException: An instance of OnFlingListener already set.
at androidx.recyclerview.widget.SnapHelper.setupCallbacks(SnapHelper.java:113)
at androidx.recyclerview.widget.SnapHelper.attachToRecyclerView(SnapHelper.java:101)
at com.akexorcist.snaptimepicker.SnapTimePickerDialog.prepare(SnapTimePickerDialog.kt:104)
at com.akexorcist.snaptimepicker.BaseSnapTimePickerDialogFragment.onActivityCreated(BaseSnapTimePickerDialogFragment.kt:44)
at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2461)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:908)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1229)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1295)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:686)
at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2057)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1847)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1802)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1709)
at androidx.fragment.app.FragmentManagerImpl$1.run(FragmentManagerImpl.java:147)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7397)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935)

Here my use case (inside a fragment)

    SnapTimePickerDialog picker = new SnapTimePickerDialog.Builder()
            .setPreselectedTime(new TimeValue(calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE)))
            .build();

    picker.setListener((hourOfDay, minute) -> {
         [...]
    }
    picker.show(getFragmentManager(), TAG);

Any help would be appreciated. Please ask me if additional info are needed. Thanks again.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.