- Notifications
You must be signed in to change notification settings - Fork1
Dialog for Android that allows pick month and year without exact day which is impossible with standard DatePickerDialog. It has customizable UI and different modes of selecting.
License
Dzmitry-Lakisau/MonthYearPickerDialog
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Have you ever been told to implement picker of month and year only? Then you know that standardDatePickerDialog requires selecting of exact day and makes selecting of month and year long. This library solves both this issues.
Sincethis andthis libraries didn't have required features for my back then enterprise projects I have written this new library fully in Kotlin with usage of ideas of above-mentioned libraries. In process I have added a few more features.
Creating usual picker is simple:
MonthYearPickerDialog.Builder(context = this,themeResId = R.style.Style_MonthYearPickerDialog,onDateSetListener = { year, month ->// do something}).build()
You will get a picker with all months and years ranging from 1970 to the current year available for selection.
You can set the initially selected month and year:
MonthYearPickerDialog.Builder( context = this,themeResId = R.style.Style_MonthYearPickerDialog,onDateSetListener = { year, month // do something},selectedYear = initialYear,selectedMonth = initialMonth).build()
You can change button's text and add dialog's title with standard method:
val dialog = MonthYearPickerDialog.Builder(this,R.style.Style_MonthYearPickerDialog_Orange,{ year, month ->// do something},initialYear,initialMonth).setNegativeButton("Return").setPositiveButton(R.string.set).build()dialog.setTitle("Select month and year")
Custom header with standard method:
val dialog = MonthYearPickerDialog.Builder(this,R.style.Style_MonthYearPickerDialog_Orange,{ year, month ->// do something},initialYear,initialMonth).setNegativeButton("Return").setPositiveButton(R.string.set).build()dialog.setCustomTitle(layoutInflater.inflate(R.layout.header, null))
There are methods in the builder to set minimum month and year (default to January and 1970 respectively):
MonthYearPickerDialog.Builder(this,R.style.Style_MonthYearPickerDialog_Orange,{ year, month ->// do something},2020,Calendar.JULY).setMinMonth(Calendar.MAY).setMinYear(2020).build()
In example below this combination is set to May 2020. With years above minimum year it's possible to select all months including below minimum month. But if after this select 2020, selected month will change to May to set combination of minimum month and year.
There are methods in the builder to set maximum month and year (default to December and current year respectively):
MonthYearPickerDialog.Builder(this,R.style.Style_MonthYearPickerDialog_Orange,{ year, month ->// do something},2022,Calendar.FEBRUARY).setMaxMonth(Calendar.MARCH).setMaxYear(2022).build()
Same logic regarding limit is applied:
Limits can be combined. Example below shows how to let selection of month in range from May 2018 to February 2022.
MonthYearPickerDialog.Builder(this,R.style.Style_MonthYearPickerDialog_Red,{ year, month ->// do something},2022,Calendar.FEBRUARY).setMinMonth(Calendar.MAY).setMinYear(2018).setMaxMonth(Calendar.FEBRUARY).setMaxYear(2022).build()
If you want to let selection of month from same range of months for every year, then there's special mode for this:
MonthYearPickerDialog.Builder(this,R.style.Style_MonthYearPickerDialog_Red,{ year, month ->// do something},2022,Calendar.JULY).setAnnualMode(true).setMinMonth(Calendar.MAY).setMinYear(2018).setMaxMonth(Calendar.AUGUST).setMaxYear(2022).build()
It will drastically change behaviour of picker comparing to the previous example:
There are mode for selection of month only and listener of when user has selected month (notice how label changes immediately after click on month without click on dialog's positive button). Also there is method to set month format:
MonthYearPickerDialog.Builder(this,R.style.Style_MonthYearPickerDialog_Red,selectedMonth = Calendar.FEBRUARY).setMinMonth(Calendar.FEBRUARY).setMaxMonth(Calendar.JULY).setMonthFormat("MMM").setMode(MonthYearPickerDialog.Mode.MONTH_ONLY).setOnMonthSelectedListener { month ->// do something}.build()
There are mode for selection of year only and listener of when user has selected year:
MonthYearPickerDialog.Builder(this,R.style.Style_MonthYearPickerDialog_Red,selectedYear = 2022).setMinYear(2010).setMode(MonthYearPickerDialog.Mode.YEAR_ONLY).setOnYearSelectedListener { month ->// do something}.build()
With this you even can transform picker into any sequential number picker:
You can style your dialog with required parameterthemeResId in the builder. Next attributes will help you to do this:
<attr name="headerBackgroundColor" format="color|reference" /><attr name="headerTextColor" format="color|reference" /><attr name="headerTextColorSelected" format="color|reference" /><attr name="monthBackgroundColorSelected" format="color|reference" /><attr name="monthTextColor" format="color|reference" /><attr name="monthTextColorDisabled" format="color|reference" /><attr name="monthTextColorSelected" format="color|reference" /><attr name="yearTextColor" format="color|reference" /><attr name="yearTextColorSelected" format="color|reference" />
Library can be fetched fromMavenCentral
Gradle
implementation 'io.github.dzmitry-lakisau:month-year-picker-dialog:1.0.0'
Maven
<dependency> <groupId>io.github.dzmitry-lakisau</groupId> <artifactId>month-year-picker-dialog</artifactId> <version>1.0.0</version> </dependency>
Copyright © 2021 Dzmitry LakisauLicensed 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.0Unless required by applicable law or agreed to in writing, softwaredistributed 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 andlimitations under the License.
About
Dialog for Android that allows pick month and year without exact day which is impossible with standard DatePickerDialog. It has customizable UI and different modes of selecting.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.