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
- 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
}
});
- Customizing appearance:
DatePicker picker = new DatePicker(this);
picker.setTextSize(16);
picker.setTextColor(Color.BLACK);
picker.setSelectedColor(Color.BLUE);
picker.setTodayColor(Color.RED);
- 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:
- Add the following to your app's
build.gradle
:
dependencies {
implementation 'com.github.aigestudio:DatePicker:2.2.0'
}
- Add the JitPack repository to your project's
build.gradle
:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
- 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" />
- 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 designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
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.
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
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot