Popularity
0.4
Stable
Activity
6.3
Declining
2
1
0

Description

Have you ever been told to implement picker of month and year only? Then you know that standard DatePickerDialog requires selecting of exact day and makes selecting of month and year long. This library solves both this issues.

Since this and this 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.

Programming language: Kotlin
License: Apache License 2.0

MonthYearPickerDialog alternatives and similar packages

Based on the "Dialog Widget" category.
Alternatively, view MonthYearPickerDialog alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of MonthYearPickerDialog or a related project?

Add another 'Dialog Widget' Package

README

Have you ever been told to implement picker of month and year only? Then you know that standard DatePickerDialog requires selecting of exact day and makes selecting of month and year long. This library solves both this issues.

Since this and this 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.

Examples of usage

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:

Styling

You can style your dialog with required parameter themeResId 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" />

Documentation

Html

Javadoc

Installation

Library can be fetched from MavenCentral

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>  

License

Copyright © 2021 Dzmitry Lakisau

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.


*Note that all licence references and agreements mentioned in the MonthYearPickerDialog README section above are relevant to that project's source code only.