Android Betterpickers

repository·master·Indexed 25 days ago

https://github.com/code-troopers/android-betterpickers

A library of custom DialogFragments for picking time, date, numbers, and other specific data types, modeled after AOSP Clock and Calendar apps. It includes components such as CalendarDatePickerDialogFragment, RadialTimePickerDialogFragment, RecurrencePickerDialogFragment, and TimeZonePickerDialogFragment, as well as builder-based pickers for expiration, HMS, and numbers. Version 3.1.0 is available via Gradle and Maven.

Tokens
4.6K
Snippets
12
Records
14
Agent score
83%

What's inside Android Betterpickers

  1. Install Android Betterpickers via Maven

    master

    To include version 1.6.0 of the library in your Maven project, add the following dependency to your pom.xml. Note that this version uses support-v4 and android-switch-backport.

    <dependency>
      <groupId>com.doomonafireball.betterpickers</groupId>
      <artifactId>library</artifactId>
      <version>1.6.0</version>
      <type>aar</type>
    </dependency>
  2. Customize Date, Expiration, HMS, Number, and Time Picker Themes

    master

    For the builder-based pickers, you can customize the following attributes in your styles.xml:

    • bpDialogBackground: The drawable (preferably 9-patch) used as the window background.
    • bpTextColor: Color (or state list) for all text.
    • bpDeleteIcon: Drawable for the delete button.
    • bpCheckIcon: Drawable for the check button (Date Picker).
    • bpKeyBackground: Drawable for keyboard buttons.
    • bpButtonBackground: Drawable for Set, Cancel, and Delete buttons.
    • bpDividerColor: Color for dividers.
    • bpKeyboardIndicatorColor: Color for the ViewPagerIndicator (Date Picker).
    <style name="MyCustomBetterPickerTheme">
        <item name="bpDialogBackground">@drawable/custom_dialog_background</item>
        <item name="bpTextColor">@color/custom_text_color</item>
        <item name="bpDeleteIcon">@drawable/ic_backspace_custom</item>
        <item name="bpCheckIcon">@drawable/ic_check_custom</item>
        <item name="bpKeyBackground">@drawable/key_background_custom</item>
        <item name="bpButtonBackground">@drawable/button_background_custom</item>
        <item name="bpDividerColor">@color/custom_divider_color</item>
        <item name="bpKeyboardIndicatorColor">@color/custom_keyboard_indicator_color</item>
    </style>
    DatePickerBuilder dpb = new DatePickerBuilder()
        .setFragmentManager(getSupportFragmentManager())
        .setStyleResId(R.style.MyCustomBetterPickerTheme);
    dpb.show();
  3. Install Android Betterpickers via Gradle

    master

    To include version 1.6.0 of the library in your Gradle project, add the following dependency to your build.gradle file.

    If your project already includes the support library and you encounter conflicts, you may need to exclude support-v4 from the betterpickers dependency.

    // Standard inclusion
    compile 'com.doomonafireball.betterpickers:library:1.6.0'
    
    // Inclusion with support-v4 exclusion to avoid conflicts
    compile ("com.doomonafireball.betterpickers:library:1.6.0") {
        exclude group: 'com.android.support', module: 'support-v4'
    }
  4. Install Android Betterpickers via Gradle or Maven

    master

    To include the Betterpickers library in your Android project, use one of the following dependency management methods. The current version is 3.1.0.

    ### Gradle
    ```groovy
    compile 'com.code-troopers.betterpickers:library:3.1.0'

    Maven

    <dependency>
      <groupId>com.code-troopers.betterpickers</groupId>
      <artifactId>library</artifactId>
      <version>3.1.0</version>
      <type>aar</type>
    </dependency>
  5. Customize Calendar Date and Radial Time Picker Themes

    master

    You can customize the appearance of the Calendar Date Picker and Radial Time Picker by creating a custom style in styles.xml using specific attributes.

    Available Attributes:

    • bpHeaderBackgroundColor
    • bpHeaderUnselectedTextColor
    • bpHeaderSelectedTextColor
    • bpBodyBackgroundColor
    • bpBodySelectedTextColor
    • bpBodyUnselectedTextColor
    • bpButtonsBackgroundColor
    • bpButtonsTextColor
    • bpPreHeaderBackgroundColor (Calendar Date Picker only)
    • bpDisabledDayTextColor (Calendar Date Picker only)
    • bpRadialBackgroundColor (Radial Time Picker only)
    • bpRadialTextColor (Radial Time Picker only)
    • bpRadialPointerColor (Radial Time Picker only)
    • bpAmPmCircleColor (Radial Time Picker only)
    <style name="MyCustomBetterPickersDialogs" parent="BetterPickersRadialTimePickerDialog.PrimaryColor">
        <item name="bpPreHeaderBackgroundColor">@color/holo_red_dark</item>
        <item name="bpHeaderBackgroundColor">@color/holo_red_light</item>
        <item name="bpHeaderSelectedTextColor">@color/holo_orange_dark</item>
        <item name="bpHeaderUnselectedTextColor">@android:color/white</item>
    
        <item name="bpBodyBackgroundColor">@color/holo_blue_dark</item>
        <item name="bpBodySelectedTextColor">@color/holo_orange_dark</item>
        <item name="bpBodyUnselectedTextColor">@android:color/white</item>
    
        <item name="bpRadialBackgroundColor">@color/holo_orange_dark</item>
        <item name="bpRadialTextColor">@color/holo_purple</item>
        <item name="bpRadialPointerColor">@android:color/black</item>
    
        <item name="bpButtonsBackgroundColor">@color/holo_green_dark</item>
        <item name="bpButtonsTextColor">@color/holo_orange_dark</item>
    </style>
    RadialTimePickerDialogFragment rtpd = new RadialTimePickerDialogFragment()
           .setOnTimeSetListener(SampleRadialTimeThemeCustom.this)
           .setThemeCustom(R.style.MyCustomBetterPickersDialogs);
    rtpd.show(getSupportFragmentManager(), FRAG_TAG_TIME_PICKER);
  6. Use the Recurrence Picker

    master

    The RecurrencePickerDialogFragment is used for selecting recurrence rules. It requires passing arguments via a Bundle including start time, timezone, and the RRULE string.

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FragmentManager fm = getSupportFragmentManager();
            Bundle bundle = new Bundle();
            Time time = new Time();
            time.setToNow();
            bundle.putLong(RecurrencePickerDialogFragment.BUNDLE_START_TIME_MILLIS, time.toMillis(false));
            bundle.putString(RecurrencePickerDialogFragment.BUNDLE_TIME_ZONE, time.timezone);
            bundle.putString(RecurrencePickerDialogFragment.BUNDLE_RRULE, mRrule);
            bundle.putBoolean(RecurrencePickerDialogFragment.BUNDLE_HIDE_SWITCH_BUTTON, true);
    
            RecurrencePickerDialogFragment rpd = new RecurrencePickerDialogFragment();
            rpd.setArguments(bundle);
            rpd.setOnRecurrenceSetListener(SampleRecurrenceBasicUsage.this);
            rpd.show(fm, FRAG_TAG_RECUR_PICKER);
        }
    });
  7. Use Builder-based Pickers (Date, Expiration, HMS, Number, Time)

    master

    Several pickers use a Builder pattern for instantiation. You must provide a FragmentManager and can optionally set a custom style resource ID.

    // Date Picker
    DatePickerBuilder dpb = new DatePickerBuilder()
            .setFragmentManager(getSupportFragmentManager())
            .setStyleResId(R.style.BetterPickersDialogFragment)
            .setYearOptional(true);
    dpb.show();
    
    // Expiration Picker
    ExpirationPickerBuilder epb = new ExpirationPickerBuilder()
              .setFragmentManager(getSupportFragmentManager())
              .setStyleResId(R.style.BetterPickersDialogFragment)
              .setMinYear(2000);
    epb.show();
    
    // HMS Picker
    HmsPickerBuilder hpb = new HmsPickerBuilder()
            .setFragmentManager(getSupportFragmentManager())
            .setStyleResId(R.style.BetterPickersDialogFragment);
    hpb.show();
    
    // Number Picker
    NumberPickerBuilder npb = new NumberPickerBuilder()
            .setFragmentManager(getSupportFragmentManager())
            .setStyleResId(R.style.BetterPickersDialogFragment)
            .setLabelText("LBS.");
    npb.show();
    
    // Time Picker
    TimePickerBuilder tpb = new TimePickerBuilder()
            .setFragmentManager(getSupportFragmentManager())
            .setStyleResId(R.style.BetterPickersDialogFragment);
    tpb.show();
  8. Use the Radial Time Picker

    master

    The RadialTimePickerDialogFragment allows users to pick time using a radial interface. You can set a start time and customize the theme and button text.

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RadialTimePickerDialogFragment rtpd = new RadialTimePickerDialogFragment()
                    .setOnTimeSetListener(SampleRadialTimeBasicUsage.this)
                    .setStartTime(10, 10)
                    .setDoneText("Yay")
                    .setCancelText("Nop")
                    .setThemeDark(true);
            rtpd.show(getSupportFragmentManager(), FRAG_TAG_TIME_PICKER);
        }
    });
  9. Use the Calendar Date Picker

    master

    The CalendarDatePickerDialogFragment provides a calendar-style date selection interface. You can customize the first day of the week, preselect a date, set a date range, and customize button text.

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CalendarDatePickerDialogFragment cdp = new CalendarDatePickerDialogFragment()
                    .setOnDateSetListener(SampleCalendarDateBasicUsage.this)
                    .setFirstDayOfWeek(Calendar.SUNDAY)
                    .setPreselectedDate(towDaysAgo.getYear(), towDaysAgo.getMonthOfYear() - 1, towDaysAgo.getDayOfMonth())
                    .setDateRange(minDate, null)
                    .setDoneText("Yay")
                    .setCancelText("Nop")
                    .setThemeDark(true);
            cdp.show(getSupportFragmentManager(), FRAG_TAG_DATE_PICKER);
        }
    });
  10. Use the Timezone Picker

    master

    The TimeZonePickerDialogFragment allows users to select a timezone. It uses a Bundle to pass the current time and timezone context.

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FragmentManager fm = getSupportFragmentManager();
            Bundle bundle = new Bundle();
            Time time = new Time();
            time.setToNow();
            bundle.putLong(TimeZonePickerDialogFragment.BUNDLE_START_TIME_MILLIS, time.toMillis(false));
            bundle.putString(TimeZonePickerDialogFragment.BUNDLE_TIME_ZONE, time.timezone);
            bundle.putString(RecurrencePickerDialogFragment.BUNDLE_RRULE, mRrule);
    
            TimeZonePickerDialogFragment tzpd = new TimeZonePickerDialogFragment();
            tzpd.setArguments(bundle);
            tzpd.setOnTimeZoneSetListener(SampleTimeZoneBasicUsage.this);
            tzpd.show(fm, FRAG_TAG_TIME_ZONE_PICKER);
        }
    });
  11. Use TimeZoneFilterTypeAdapter to filter time zone results

    master

    The TimeZoneFilterTypeAdapter is a BaseAdapter used to display and filter time zone data. It can be initialized with a Context, a TimeZoneData object, and an implementation of OnSetFilterListener.

    It supports several filter types defined by constants:

    • FILTER_TYPE_EMPTY (-1): No results found.
    • FILTER_TYPE_NONE (0): The filter constraint is empty.
    • FILTER_TYPE_COUNTRY (1): Results filtered by country name.
    • FILTER_TYPE_STATE (2): Results filtered by state (implementation pending in source).
    • FILTER_TYPE_GMT (3): Results filtered by GMT offset (e.g., searching "GMT+5" or "-3").