Giter Site home page Giter Site logo

dzmitry-lakisau / monthyearpickerdialog Goto Github PK

View Code? Open in Web Editor NEW
9.0 1.0 0.0 5.25 MB

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: Apache License 2.0

Kotlin 100.00%
android-month-and-year-picker android-month-picker android-only-month-picker android-year-picker android-datepicker android-pickerdialog datepicker-dialog monthpicker month-picker picker

monthyearpickerdialog's Introduction

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.

monthyearpickerdialog's People

Contributors

dzmitry-lakisau avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

monthyearpickerdialog's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.