Convert Figma logo to code with AI

AigeStudio logoDatePicker

Useful and powerful date picker for android

1,861
414
1,861
65

Top Related Projects

Pick a date or time on Android in style

Standalone Android widget for picking a single date from a calendar view.

[NO LONGER MAINTAINED] Android library for better Picker DialogFragments

A Material design back port of Android's CalendarView

A highly customizable calendar view and compose library for Android and Kotlin Multiplatform.

A better calendar for Android

Quick Overview

AigeStudio/DatePicker is an Android library that provides a customizable date picker widget. It offers a flexible and visually appealing way to select dates in Android applications, with support for various calendar styles and customization options.

Pros

  • Highly customizable appearance and behavior
  • Supports both single date and date range selection
  • Offers multiple calendar styles (Gregorian, Lunar, etc.)
  • Smooth animations and intuitive user interface

Cons

  • Limited documentation, especially for advanced customization
  • Some reported issues with compatibility on newer Android versions
  • Infrequent updates and maintenance
  • Lack of extensive localization support

Code Examples

  1. Basic usage of DatePicker:
DatePicker picker = new DatePicker(this);
picker.setDate(2023, 5, 15);
picker.setOnDateSelectedListener(new DatePicker.OnDateSelectedListener() {
    @Override
    public void onDateSelected(List<String> date) {
        // Handle selected date
    }
});
  1. Customizing appearance:
DatePicker picker = new DatePicker(this);
picker.setTextSize(16);
picker.setTextColor(Color.BLACK);
picker.setSelectedColor(Color.BLUE);
picker.setTodayColor(Color.RED);
  1. Setting up date range selection:
DatePicker picker = new DatePicker(this);
picker.setPickerMode(DatePicker.PICKER_MODE_RANGE);
picker.setOnDateSelectedListener(new DatePicker.OnDateSelectedListener() {
    @Override
    public void onDateSelected(List<String> dates) {
        // Handle selected date range
    }
});

Getting Started

To use AigeStudio/DatePicker in your Android project:

  1. Add the following to your app's build.gradle:
dependencies {
    implementation 'com.github.aigestudio:DatePicker:2.2.0'
}
  1. Add the JitPack repository to your project's build.gradle:
allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
  1. Use the DatePicker in your layout XML or create it programmatically in your Activity/Fragment:
<cn.aigestudio.datepicker.views.DatePicker
    android:id="@+id/datePicker"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
  1. Customize and set up the DatePicker in your Java/Kotlin code as shown in the code examples above.

Competitor Comparisons

Pick a date or time on Android in style

Pros of MaterialDateTimePicker

  • Follows Material Design guidelines, providing a modern and consistent UI
  • Supports both date and time picker functionality in a single library
  • Offers more customization options for colors, themes, and styles

Cons of MaterialDateTimePicker

  • Limited to Android platform, while DatePicker supports both Android and iOS
  • May have a steeper learning curve due to more complex API and customization options
  • Larger library size compared to DatePicker, which could impact app performance

Code Comparison

MaterialDateTimePicker:

Calendar now = Calendar.getInstance();
DatePickerDialog dpd = DatePickerDialog.newInstance(
    this,
    now.get(Calendar.YEAR),
    now.get(Calendar.MONTH),
    now.get(Calendar.DAY_OF_MONTH)
);
dpd.show(getFragmentManager(), "Datepickerdialog");

DatePicker:

DatePicker picker = new DatePicker(this);
picker.setDate(2015, 11, 11);
picker.setOnDateSelectedListener(new DatePicker.OnDateSelectedListener() {
    @Override
    public void onDateSelected(int year, int month, int day) {
        // Handle date selection
    }
});

Both libraries offer date picking functionality, but MaterialDateTimePicker provides a more Material Design-compliant interface and additional features like time picking. DatePicker, on the other hand, offers a simpler API and cross-platform support. The choice between the two depends on specific project requirements and design preferences.

Standalone Android widget for picking a single date from a calendar view.

Pros of android-times-square

  • More actively maintained with recent updates
  • Backed by Square, a well-known company in the tech industry
  • Extensive documentation and examples provided

Cons of android-times-square

  • Limited customization options compared to DatePicker
  • Focuses primarily on calendar view, lacking additional date/time selection features
  • Larger library size due to additional dependencies

Code Comparison

DatePicker:

DatePicker picker = new DatePicker(this);
picker.setDate(System.currentTimeMillis());
picker.setOnDateSelectedListener(new DatePicker.OnDateSelectedListener() {
    @Override
    public void onDateSelected(List<String> date) {
        // Handle date selection
    }
});

android-times-square:

CalendarPickerView calendar = findViewById(R.id.calendar_view);
Calendar nextYear = Calendar.getInstance();
nextYear.add(Calendar.YEAR, 1);
calendar.init(new Date(), nextYear.getTime())
    .withSelectedDate(new Date());
calendar.setOnDateSelectedListener(new CalendarPickerView.OnDateSelectedListener() {
    @Override
    public void onDateSelected(Date date) {
        // Handle date selection
    }
});

Both libraries offer easy-to-use date selection functionality, but DatePicker provides a more customizable interface while android-times-square focuses on a clean, Material Design-inspired calendar view. The choice between the two depends on specific project requirements and design preferences.

[NO LONGER MAINTAINED] Android library for better Picker DialogFragments

Pros of android-betterpickers

  • Offers a wider variety of pickers (date, time, number, recurrence, etc.)
  • More customizable appearance and theming options
  • Better support for different Android versions and screen sizes

Cons of android-betterpickers

  • Less frequent updates and maintenance compared to DatePicker
  • Slightly more complex implementation due to its broader feature set
  • Larger library size, which may impact app size

Code Comparison

DatePicker implementation:

DatePicker picker = new DatePicker(this);
picker.setDate(2023, 5, 15);
picker.setOnDateSelectedListener(new DatePicker.OnDateSelectedListener() {
    @Override
    public void onDateSelected(int year, int month, int day) {
        // Handle selected date
    }
});

android-betterpickers implementation:

Calendar now = Calendar.getInstance();
DatePickerBuilder dpb = new DatePickerBuilder()
    .setFragmentManager(getSupportFragmentManager())
    .setStyleResId(R.style.BetterPickersDialogFragment)
    .setYear(now.get(Calendar.YEAR))
    .setMonthOfYear(now.get(Calendar.MONTH))
    .setDayOfMonth(now.get(Calendar.DAY_OF_MONTH))
    .setCallBack(this)
    .addDatePickerDialogHandler(new DatePickerDialogFragment.DatePickerDialogHandler() {
        @Override
        public void onDialogDateSet(int year, int monthOfYear, int dayOfMonth) {
            // Handle selected date
        }
    });
dpb.show();

A Material design back port of Android's CalendarView

Pros of material-calendarview

  • More customizable appearance with Material Design principles
  • Better support for range selection and multiple date selection
  • Smoother animations and transitions between months

Cons of material-calendarview

  • Larger library size, potentially impacting app performance
  • Steeper learning curve due to more complex API
  • Less flexibility in terms of custom layouts compared to DatePicker

Code Comparison

DatePicker:

DatePicker datePicker = (DatePicker) findViewById(R.id.date_picker);
datePicker.setDate(2023, 5, 1);
datePicker.setOnDateSelectedListener(new DatePicker.OnDateSelectedListener() {
    @Override
    public void onDateSelected(int year, int month, int day) {
        // Handle date selection
    }
});

material-calendarview:

MaterialCalendarView calendarView = findViewById(R.id.calendarView);
calendarView.setSelectedDate(CalendarDay.today());
calendarView.setOnDateSelectedListener(new OnDateSelectedListener() {
    @Override
    public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {
        // Handle date selection
    }
});

Both libraries offer date picking functionality, but material-calendarview provides a more modern look and feel with additional features. DatePicker is simpler to implement and may be suitable for basic use cases, while material-calendarview offers more advanced customization options and better supports complex date selection scenarios.

A highly customizable calendar view and compose library for Android and Kotlin Multiplatform.

Pros of Calendar

  • Written in Kotlin, offering modern language features and better null safety
  • Supports both vertical and horizontal scrolling
  • More customizable with flexible day view layouts and custom month headers

Cons of Calendar

  • Steeper learning curve due to more complex API
  • Requires more setup code for basic functionality
  • Limited built-in styling options compared to DatePicker

Code Comparison

DatePicker:

DatePicker datePicker = (DatePicker) findViewById(R.id.date_picker);
datePicker.setDate(2023, 5, 15);
datePicker.setOnDateSelectedListener(new DatePicker.OnDateSelectedListener() {
    @Override
    public void onDateSelected(Date date) {
        // Handle date selection
    }
});

Calendar:

val calendarView = findViewById<CalendarView>(R.id.calendarView)
calendarView.setup(startMonth, endMonth, firstDayOfWeek)
calendarView.scrollToMonth(currentMonth)
calendarView.dayBinder = object : DayBinder<DayViewContainer> {
    override fun create(view: View) = DayViewContainer(view)
    override fun bind(container: DayViewContainer, day: CalendarDay) {
        // Bind day view
    }
}

The Calendar library offers more flexibility but requires more setup code. DatePicker provides a simpler API for basic date selection functionality, while Calendar allows for more advanced customization and layout options.

A better calendar for Android

Pros of Caldroid

  • More customizable appearance with support for custom cell views and styles
  • Better handling of date ranges and multi-date selection
  • Extensive documentation and examples provided in the README

Cons of Caldroid

  • Larger library size and potentially higher memory footprint
  • Less intuitive API for basic date picking functionality
  • Fewer animation options for date transitions

Code Comparison

DatePicker:

DatePicker picker = new DatePicker(this);
picker.setDate(2023, 5, 15);
picker.setOnDateSelectedListener(new DatePicker.OnDateSelectedListener() {
    @Override
    public void onDateSelected(Date date) {
        // Handle date selection
    }
});

Caldroid:

CaldroidFragment caldroidFragment = new CaldroidFragment();
Bundle args = new Bundle();
args.putInt(CaldroidFragment.MONTH, 5);
args.putInt(CaldroidFragment.YEAR, 2023);
caldroidFragment.setArguments(args);
caldroidFragment.setSelectedDate(new Date());
caldroidFragment.setOnDateSelectedListener(new CaldroidListener() {
    @Override
    public void onSelectDate(Date date, View view) {
        // Handle date selection
    }
});

Both libraries offer date picking functionality, but Caldroid provides more advanced features at the cost of a slightly more complex setup. DatePicker has a simpler API for basic use cases, while Caldroid offers greater flexibility for customization and advanced date selection scenarios.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Android Arsenal

You can jump to EN README after CN if you don't read chinese.


Here is begin of CN document.

日历选择器

灰常灰常简单的Android日历选择器(注:暂不支持横屏操作,农历节气有一到两天的误差)

Android API 版本要求

API 1

API 11

API 1

版本迭代

1.0.0 beta

  • 横屏暂未考虑,竖屏暂未发现问题,项目使用中

1.1.2 release

  • 增加日期选择时弹性动画
  • 修复设置主色调后农历颜色不改变BUG

1.1.3 release

  • 不管是横屏还是竖屏,你必须总为该控件指定一个确切宽度比如320dp或者填充父布局,大多数情况下该日期选择器都是在dialog中使用,默认dialog宽度是填充屏幕宽度的,你可以参照Demo来更改diaolog的宽度。

2.0.0 stable LTS

  • API版本支持到1,对于大于等于API11的版本支持动画显示
  • 增加呼声很高的单选模式支持 目前支持两种模式 单选和多选
  • 增加周次标题的显示
  • 支持上下滑动切换年份 左右滑动切换月份
  • 节日文本显示从单行调整为两行
  • 调整UI色调符合material design
  • 对于天朝月历,增加假期、补休的标识
  • 增加呼声很高的对今天日期的标识
  • 支持默认年月设定
  • 在现有月视图的基础上分割出五个区域提供自定义装饰物的绘制
  • 在现有月视图的基础上分割出一个背景层提供背景装饰物的绘制
  • 内置中文和英语两种语言,根据当前系统语言环境自动切换
  • 支持自定义国家语言扩展
  • 内置中国和美国两国节假日显示
  • 支持多国假期节日显示扩展
  • 增强主题色调扩展定制更易于操作
  • 优化天朝农历和节气算法 农历显示范围从1900-2100
  • 优化代码逻辑提升执行效率
  • 该版本后不再作重大更新

2.0.1 stable

  • BugFix:忘记更改测试代码导致的显示问题

2.0.2 stable

  • BugFix:顶部装饰物绘制错位问题

2.1.0

  • 修改装饰物绘制逻辑,可根据日期在同一区域绘制不同装饰物

2.2.0

  • BugFix:最后一个月份当前日期无法显示问题

预览图

如何集成到项目

方式一 直接从maven center compile

compile 'cn.aigestudio.datepicker:DatePicker:2.2.0'

方式二 手动导入

步骤一

将DatePicker这个Module导入你的Project中

步骤二

在你Project的settings.gradle文件中增加如下内容:

include ':DatePicker'

这里要注意的是在一些gradle版本中需要以英文逗号的方式追加Module:

include ':YourMoudle',':DatePicker'

添加后当出现“sycn now”提示时点击同步即可

步骤三

在你项目的build.gradle文件的dependencies区域中添加如下内容:

compile project(':DatePicker')

如何使用

简单使用

一旦将DatePicker集成到项目后你便可以像普通控件那样使用它。

如果你需要获取DatePicker日期选择后返回的数据你需要为DatePicker设置一个OnDateSelectedListener监听器:

......
DatePicker picker = (DatePicker) findViewById(R.id.main_dp);
picker.setDate(2015, 7);
picker.setOnDateSelectedListener(new DatePicker.OnDateSelectedListener() {
    @Override
    public void onDateSelected(List<String> date) {
        String result = "";
        Iterator iterator = date.iterator();
        while (iterator.hasNext()) {
            result += iterator.next();
            if (iterator.hasNext()) {
                result += "\n";
            }
        }
        Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
    }
});
......

setDate方法允许你设置当前月历显示的年月。注意该方法必须调用,也就是说你必须为DatePicker指定一个确切年月

默认情况下DatePicker 2.0的选择模式为多选模式,你可以通过setMode方法来设置DatePicker的选择模式,该方法接受一个DPMode类型的枚举值,DatePicker 2.0支持两种选择模式:单选DPMode.SINGLE和多选DPMode.MULTIPLE,设置方式如下:

......
DatePicker picker = (DatePicker) findViewById(R.id.main_dp);
picker.setDate(2015, 7);
picker.setMode(DPMode.SINGLE);
......

在单选模式下,如果你想要获取DatePicker日期选择后返回的数据,你就不能再将DatePicker的监听设置为OnDateSelectedListener而应该设置为OnDatePickedListener:

......
DatePicker picker = (DatePicker) findViewById(R.id.main_dp);
picker.setDate(2015, 7);
picker.setMode(DPMode.SINGLE);
picker.setOnDatePickedListener(new DatePicker.OnDatePickedListener() {
    @Override
    public void onDatePicked(String date) {
        Toast.makeText(MainActivity.this, date, Toast.LENGTH_LONG).show();
    }
});
......

这里需要注意的是,你不能将DatePicker设置为多选或单选模式的情况下又设置为单选或多选,在一个实例中只能接受一种选择模式

选择后的日期将会以列表(多选模式下)或字符串(单选模式下)的形式返回,日期字符串的格式为:

yyyy-M-d

比如:2015-3-28

高级定制

DatePicker 2.0默认了一套显示机制,对于天朝月历而言,2015年的假期与补休都会被不同的背景圆颜色所标识,对于其他国家月历而言只有假期会被标识,当然,在某些情况下你还想在某些特定的日期有自己的显示标识,DatePicker 2.0在原有绘制层的基础上分割出了一个背景层,提供给用户绘制自己想要的标识物。比如你想在2015-7-1,2015-7-8,2015-7-16这三个日期上绘制一个不同的背景圆,首先你要通过DPCManager的setDecorBG方法设置一个日期列表,该列表包含了需要绘制背景标识的日期(在没有特殊说明的情况下,DatePicker 2.0中所使用到的日期格式均与上述一致):

......
List<String> tmp = new ArrayList<>();
tmp.add("2015-7-1");
tmp.add("2015-7-8");
tmp.add("2015-7-16");
DPCManager.getInstance().setDecorBG(tmp);
......

然后你就可以调用DatePicker的setDPDecor方法为DatePicker绘制装饰物背景:

......
List<String> tmp = new ArrayList<>();
tmp.add("2015-7-1");
tmp.add("2015-7-8");
tmp.add("2015-7-16");
DPCManager.getInstance().setDecorBG(tmp);

DatePicker picker = (DatePicker) findViewById(R.id.main_dp);
picker.setDate(2015, 7);
picker.setDPDecor(new DPDecor() {
    @Override
    public void drawDecorBG(Canvas canvas, Rect rect, Paint paint) {
        paint.setColor(Color.RED);
        canvas.drawCircle(rect.centerX(), rect.centerY(), rect.width() / 2F, paint);
    }
});
......

这里我们在2015-7-1,2015-7-8,2015-7-16这三个日期上绘制一个红色的背景圆:

DatePicker 2.0中所提供的背景层位于日期文本的下方所有其他背景的上方,也就是说在所有的背景层中,你所自定义的背景拥有最高展示的优先级

当然,有些情况下光是绘制一个背景层往往不是不够的,DatePicker 2.0充分考虑到这一点,开放了位于前景层的五个装饰区域给用户绘制不同的装饰物,这五个装饰区域的位置大致如下:

如上图所示,DatePicker 2.0开放了位于前景层的A、B、C、D、E五个区域让用于有机会在这些前景层的这些区域绘制一些装饰物,使用方法也非常简单,与绘制背景装饰物的逻辑类似,这里以分别在2015-7-5,2015-7-10这两个日期的左上和又上角绘制不同颜色形状的图形为例,首先你需要做的第一步与绘制背景装饰物一样,先调用DPCManager的相关方法初始化日期数据:

......
List<String> tmpTL = new ArrayList<>();
tmpTL.add("2015-7-5");
DPCManager.getInstance().setDecorTL(tmpTL);

List<String> tmpTR = new ArrayList<>();
tmpTR.add("2015-7-10");
DPCManager.getInstance().setDecorTR(tmpTR);
......

这里补充一点,从上面装饰区域的示例图中其实可以看到A、B、C、D、E五个区域分别表示的是左上角(Top left)、顶部(Top)、右上角(Top right)、左边(Left)、右边(Right),与之对应的设置日期数据与绘制的方法也采用不同的命名来区分,比如上面的代码中我们为左上角TL和右上角TR设置标识日期数据就分别用到了setDecorTL方法和setDecorTR方法,当然,在绘制的时候我们也需要在相应的方法中绘制:

......
List<String> tmpTL = new ArrayList<>();
tmpTL.add("2015-7-5");
DPCManager.getInstance().setDecorTL(tmpTL);

List<String> tmpTR = new ArrayList<>();
tmpTR.add("2015-7-10");
DPCManager.getInstance().setDecorTR(tmpTR);

DatePicker picker = (DatePicker) findViewById(R.id.main_dp);
picker.setDate(2015, 7);
picker.setDPDecor(new DPDecor() {
    @Override
    public void drawDecorTL(Canvas canvas, Rect rect, Paint paint) {
        paint.setColor(Color.GREEN);
        canvas.drawRect(rect, paint);
    }

    @Override
    public void drawDecorTR(Canvas canvas, Rect rect, Paint paint) {
        paint.setColor(Color.BLUE);
        canvas.drawCircle(rect.centerX(), rect.centerY(), rect.width() / 2, paint);
    }
});
picker.setOnDateSelectedListener(new DatePicker.OnDateSelectedListener() {
    @Override
    public void onDateSelected(List<String> date) {
        String result = "";
        Iterator iterator = date.iterator();
        while (iterator.hasNext()) {
            result += iterator.next();
            if (iterator.hasNext()) {
                result += "\n";
            }
        }
        Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
    }
});
......

绘制效果如下:

注意!!!这里非常重要的一点是你必须在DatePicker显示前设置DPCManager中的相关数据!!!

功能扩展

DatePicker 2.0提供了三个Manager来管理控件的各项功能,其中DPCManager用于对日期数据的加载,这在上面的介绍中有所提及这里不再多说;DPLManager用于对语言环境的控制,DatePicker 2.0内置中文和英语两种语言并会根据当前系统的语言环境进行自动切换,如果你想扩展更多的语言,只需继承DPLManager并重写相关方法即可,同时你还必须在DPLManager的单例方法中增加相关逻辑以便DatePicker能识别你的语言:

    /**
     * 获取日历语言管理器
     * 
     * Get DatePicker language manager
     *
     * @return 日历语言管理器 DatePicker language manager
     */
    public static DPLManager getInstance() {
        if (null == sLanguage) {
            String locale = Locale.getDefault().getLanguage().toLowerCase();
            if (locale.equals("zh")) {
                sLanguage = new CN();
            } else {
                sLanguage = new EN();
            }
        }
        return sLanguage;
    }

最后我们还提供了一个DPTManager用于对DatePicker整体色调的控制,如果你想实现自己的主题色调,只需继承DPTheme并实现相关方法,然后再调用DPTManager的initCalendar方法传入你自定义的DPTheme对象即可。这里与DPCManager不同的是主题的初始化(也就是说DPTManager的initCalendar方法的调用)必须在DatePicker构造前完成!!!这点与DPCManager在DatePicker显示前调用不同!!!


这里开始是英文文档

DatePicker

A simple date picker for android~~(note:it doesn't work with horizontal view yet,and it has one or two days deviation of solar term)~~

Android API Needs

API 1

API 11

API 1

Version

1.0.0 beta

  • Doesn't work with horizontal view yet,but vertical right.

1.1.2 release

  • Add animation when date picked.
  • Bugfix:color does not change when main color set.

1.1.3 release

  • You must specify a exactly width like 320dp or match_parent whether portrait or landscape, In most cases the datepicker is use with dialog, and it will match screen in default mode, you can refer to demo to change dialog width.

2.0.0 stable LTS

  • API Support to 1, display the animation on API 11 or above.
  • Support single and multiple choice
  • Add week view
  • Support slide up and down to swith years and left and right for months
  • Festival text display in two lines
  • Change color for material design
  • Add specific background color for holidays and deferred holidays when you use China calendar
  • Add specific background color for today in calendar
  • Support specify current year and month
  • Provide five areas in foreground to draw custom decor
  • Provide a area in background to draw custom background decor
  • Preset chinese and english language display based on current language environment auto swith
  • Support custom language extends
  • Preset China and America festival display
  • Support custom festival extends
  • Support custom theme color extends

2.0.1 stable

BugFix:Something display problem

2.0.2 stable

BugFix:The decor of top area can not be display

2.1.0

  • Draw different decorate according to date.

2.2.0

  • BugFix:Current day in last month can not display.

Preview

How to add to your project

Method A:compile from maven center

compile 'cn.aigestudio.datepicker:DatePicker:2.2.0'

Method B:Help yourself

step 1

import DatePicker lib to your project

step 2

Add something like below in your settings.gradle file of project:

include ':DatePicker'

Note that in some other gradle version you many add module like below:

include ':YourMoudle',':DatePicker'

Click 'sycn now' when it appear after module add.

step 3

Add something like below in your build.gradle file of project:

compile project(':DatePicker')

Usage

Simple use

Once you add DatePicker to your project you can use it like other views which android define.

You need to set a OnDateSelectedListener callback if you want to obtain dates when dates picked.

DatePicker picker = (DatePicker) findViewById(R.id.main_dp);
picker.setDate(2015, 7);
picker.setOnDateSelectedListener(new DatePicker.OnDateSelectedListener() {
    @Override
    public void onDateSelected(List<String> date) {
        String result = "";
        Iterator iterator = date.iterator();
        while (iterator.hasNext()) {
            result += iterator.next();
            if (iterator.hasNext()) {
                result += "\n";
            }
        }
        Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
    }
});

setDate method to set year and month current display.note: setDate method must be call, that's mean you must set a exactly year and month for DatePicker.

By default the select mode of DatePicker 2.0 is multiple, you can call method setMode to change the select mode, it accept a enum value type of DPMode:

......
DatePicker picker = (DatePicker) findViewById(R.id.main_dp);
picker.setDate(2015, 7);
picker.setMode(DPMode.SINGLE);
......

if you want to obtain date when date picked, you can not set the OnDateSelectedListener like below, you have to set the OnDatePickedListener:

......
DatePicker picker = (DatePicker) findViewById(R.id.main_dp);
picker.setDate(2015, 7);
picker.setMode(DPMode.SINGLE);
picker.setOnDatePickedListener(new DatePicker.OnDatePickedListener() {
    @Override
    public void onDatePicked(String date) {
        Toast.makeText(MainActivity.this, date, Toast.LENGTH_LONG).show();
    }
});
......

Pay attention to this, you can not set the select mode both single and multiple.

The dates you picker will return in the form of string list(In multiple select) or string(In single select),format of string like below:

yyyy-M-d

For example:2015-3-28

Advanced customization

DatePicker 2.0 has a default display mechanism, there are different color of background circle to mark date of holidays and deferred holidays when you use china calendar, for other countries, only holidays will be marked, But in some cases, you may want to draw your mark of specific dates, DatePicker 2.0 provided a backgroung layer to user to draw your own background. For example, you may want to draw different color circle on 2015-7-1 2015-7-8 and 2015-7-16, the first thing you need to do is call the setDecorBG of DPCManager to set these dates:

......
List<String> tmp = new ArrayList<>();
tmp.add("2015-7-1");
tmp.add("2015-7-8");
tmp.add("2015-7-16");
DPCManager.getInstance().setDecorBG(tmp);
......

And then you can draw the different background through setDPDecor method of DatePicker:

......
List<String> tmp = new ArrayList<>();
tmp.add("2015-7-1");
tmp.add("2015-7-8");
tmp.add("2015-7-16");
DPCManager.getInstance().setDecorBG(tmp);

DatePicker picker = (DatePicker) findViewById(R.id.main_dp);
picker.setDate(2015, 7);
picker.setDPDecor(new DPDecor() {
    @Override
    public void drawDecorBG(Canvas canvas, Rect rect, Paint paint) {
        paint.setColor(Color.RED);
        canvas.drawCircle(rect.centerX(), rect.centerY(), rect.width() / 2F, paint);
    }
});
......

In this case, we draw a red circle background to mark date 2015-7-1 2015-7-8 2015-7-16:

The custom background layer DatePicker 2.0 provided is below text layer and above all other background layer.

Of course you may want to draw something on foreground, DatePicker 2.0 provide five areas of a foreground layer like below:

You can draw differen graph on these area if you want. The draw logic of this like draw background. For example, we draw a rectangle on the top left of date 2015-7-5 and draw a circle on th top right of date 2015-7-10:

......
List<String> tmpTL = new ArrayList<>();
tmpTL.add("2015-7-5");
DPCManager.getInstance().setDecorTL(tmpTL);

List<String> tmpTR = new ArrayList<>();
tmpTR.add("2015-7-10");
DPCManager.getInstance().setDecorTR(tmpTR);
......

And then call the setDPDecor method of DatePicker and overwrite corresponding method:

......
List<String> tmpTL = new ArrayList<>();
tmpTL.add("2015-7-5");
DPCManager.getInstance().setDecorTL(tmpTL);

List<String> tmpTR = new ArrayList<>();
tmpTR.add("2015-7-10");
DPCManager.getInstance().setDecorTR(tmpTR);

DatePicker picker = (DatePicker) findViewById(R.id.main_dp);
picker.setDate(2015, 7);
picker.setDPDecor(new DPDecor() {
    @Override
    public void drawDecorTL(Canvas canvas, Rect rect, Paint paint) {
        paint.setColor(Color.GREEN);
        canvas.drawRect(rect, paint);
    }

    @Override
    public void drawDecorTR(Canvas canvas, Rect rect, Paint paint) {
        paint.setColor(Color.BLUE);
        canvas.drawCircle(rect.centerX(), rect.centerY(), rect.width() / 2, paint);
    }
});
picker.setOnDateSelectedListener(new DatePicker.OnDateSelectedListener() {
    @Override
    public void onDateSelected(List<String> date) {
        String result = "";
        Iterator iterator = date.iterator();
        while (iterator.hasNext()) {
            result += iterator.next();
            if (iterator.hasNext()) {
                result += "\n";
            }
        }
        Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
    }
});
......

Function expansion

DatePicker 2.0 provide three manager to manage differen function, we use DPCManager to manage date and calendar; Use DPLManager to manage language; Use DPTManager to manage theme color, all of these manager support custom, you can find the usage in notes.


LICENSE

Copyright 2014-2015 AigeStudio(https://github.com/AigeStudio)

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.