Home

Awesome

BottomSheetPickers

Download

BottomSheetPickers is a library of new date and time pickers for Android, supporting API level 14 and up.

Date Picker

<img src="screenshots/paging-date-picker-light.png" width="180" height="320"> <img src="screenshots/month-picker-light.png" width="180" height="320"> <img src="screenshots/year-picker-light.png" width="180" height="320">

<img src="screenshots/paging-date-picker-dark.png" width="180" height="320"> <img src="screenshots/month-picker-dark.png" width="180" height="320"> <img src="screenshots/year-picker-dark.png" width="180" height="320">

Time Pickers

Number Pad

<img src="screenshots/number-pad-12h-light.png" width="180" height="320"> <img src="screenshots/number-pad-24h-light.png" width="180" height="320"> <img src="screenshots/number-pad-12h-dark.png" width="180" height="320"> <img src="screenshots/number-pad-24h-dark.png" width="180" height="320">

Grid Picker

<img src="screenshots/12h-grid-light.png" width="180" height="320"> <img src="screenshots/minutes-grid-light.png" width="180" height="320"> <img src="screenshots/24h-grid-light.png" width="180" height="320">

<img src="screenshots/12h-grid-dark.png" width="180" height="320"> <img src="screenshots/minutes-grid-dark.png" width="180" height="320"> <img src="screenshots/24h-grid-dark.png" width="180" height="320">

Table of Contents

  1. Installation
  2. Usage
    1. Implement Callbacks
    2. Create Pickers
    3. Show the Dialog
    4. Theming
    5. Additional Options
      1. Basic Options
      2. DatePickerDialog and DatePickerDialog.Builder
      3. NumberPadTimePickerDialog and NumberPadTimePickerDialog.Builder
      4. GridTimePickerDialog and GridTimePickerDialog.Builder
      5. Setters Only
  3. Attribution
  4. License

Installation

This library depends on the appcompat-v7, design, and gridlayout-v7 support libraries. It is recommended that you exclude them from the library and add those dependencies on your own. This will ensure both your project and this library are using the same versions of the support libraries.

dependencies {
    // Recommended
    compile('com.philliphsu:bottomsheetpickers:2.4.1') {
        exclude group: 'com.android.support', module: 'appcompat-v7'
        exclude group: 'com.android.support', module: 'design'
        exclude group: 'com.android.support', module: 'gridlayout-v7'
    }
    compile 'com.android.support:appcompat-v7:[LATEST_VERSION]'
    compile 'com.android.support:design:[LATEST_VERSION]'
    compile 'com.android.support:gridlayout-v7:[LATEST_VERSION]'

    // Standard -- not recommended!
    compile 'com.philliphsu:bottomsheetpickers:2.4.1'
}

Usage

You must be using a android.support.v4.app.FragmentActivity or android.support.v4.app.Fragment. The pickers are indirect subclasses of android.support.v4.app.DialogFragment.

Implement Callbacks

To retrieve the date or time set in the pickers, implement an appropriate callback interface.

@Override
public void onDateSet(DatePickerDialog dialog, int year, int monthOfYear, int dayOfMonth) {
    Calendar cal = new java.util.GregorianCalendar();
    cal.set(Calendar.YEAR, year);
    cal.set(Calendar.MONTH, monthOfYear);
    cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
    mText.setText("Date set: " + DateFormat.getDateFormat(this).format(cal.getTime()));
}

@Override
public void onTimeSet(ViewGroup viewGroup, int hourOfDay, int minute) {
    Calendar cal = new java.util.GregorianCalendar();
    cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
    cal.set(Calendar.MINUTE, minute);
    mText.setText("Time set: " + DateFormat.getTimeFormat(this).format(cal.getTime()));
}

Create Pickers

Calendar now = Calendar.getInstance();
// As of version 2.3.0, `BottomSheetDatePickerDialog` is deprecated.
DatePickerDialog date = DatePickerDialog.newInstance(
    MainActivity.this,
    now.get(Calendar.YEAR),
    now.get(Calendar.MONTH),
    now.get(Calendar.DAY_OF_MONTH));

// Configured according to the system preference for 24-hour time.
NumberPadTimePickerDialog pad = NumberPadTimePickerDialog.newInstance(MainActivity.this);
// Alternatively, you can set 24-hour mode on your own.
boolean is24HourMode = ...
NumberPadTimePickerDialog pad = NumberPadTimePickerDialog.newInstance(MainActivity.this, is24HourMode);

GridTimePickerDialog grid = GridTimePickerDialog.newInstance(
    MainActivity.this,
    now.get(Calendar.HOUR_OF_DAY),
    now.get(Calendar.MINUTE),
    DateFormat.is24HourFormat(MainActivity.this));

As of version 2.3.0, you have the option to create pickers using the Builder pattern. This is helpful if you want to chain together additional options before creating the dialog. Of course, you can continue creating a dialog with newInstance() as usual, and then just call setters on the dialog itself.

DatePickerDialog date = new DatePickerDialog.Builder(
    MainActivity.this,
    now.get(Calendar.YEAR),
    now.get(Calendar.MONTH),
    now.get(Calendar.DAY_OF_MONTH))
    /* ... Set additional options ... */
    .build();

// Configured according to the system preference for 24-hour time.
NumberPadTimePickerDialog pad = new NumberPadTimePickerDialog.Builder(MainActivity.this)
    /* ... Set additional options ... */
    .build();
// Alternatively, you can set 24-hour mode on your own.
NumberPadTimePickerDialog pad = new NumberPadTimePickerDialog.Builder(MainActivity.this, is24HourMode)
    /* ... Set additional options ... */
    .build();

GridTimePickerDialog grid = new GridTimePickerDialog.Builder(
    MainActivity.this,
    now.get(Calendar.HOUR_OF_DAY),
    now.get(Calendar.MINUTE),
    DateFormat.is24HourFormat(MainActivity.this))
    /* ... Set additional options ... */
    .build();

Show the Dialog

Pass in a android.support.v4.app.FragmentManager to the dialog's show().

// For a `android.support.v4.app.FragmentActivity`:
dialog.show(getSupportFragmentManager(), TAG);

// For a `android.support.v4.app.Fragment`:
dialog.show(getFragmentManager(), TAG);

Theming

The pickers automatically use your current theme's colorAccent defined in your styles.xml.

You can specify whether to use a light (default) or dark theme:

<item name="themeDark">true</item>

Additional Options

These additional APIs can be used to customize the appearance of the picker. They are available both as setter methods and as Builder options, unless otherwise noted.

Basic Options

These APIs are available in all pickers.

DatePickerDialog and DatePickerDialog.Builder

NumberPadTimePickerDialog and NumberPadTimePickerDialog.Builder

GridTimePickerDialog and GridTimePickerDialog.Builder

Setters Only

These are only available as setter methods in NumberPadTimePickerDialog.

Attribution

This library is based on code from the following AOSP repositories:

License

Copyright 2017 Phillip Hsu

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License 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.