MonthAndYearPicker Android Library

repository·master·Indexed 41 days ago

https://github.com/premkumarroyal/monthandyearpicker

An Android library for selecting specific months, years, or both. It features a configurable MonthPickerDialog and MonthPickerView with support for range constraints, custom XML styling, and event listeners for date selection and changes. Version 1.3.0.

Tokens
2.9K
Snippets
7
Records
15
Agent score
51%

What's inside MonthAndYearPicker

  1. Install MonthAndYearPicker via Gradle or Maven

    master

    To use the MonthAndYearPicker library in your Android project, add the dependency to your build configuration.

    Gradle: Add the following line to your build.gradle file:

    compile 'com.whiteelephant:monthandyearpicker:1.3.0'

    Maven: Add the following dependency to your pom.xml:

    <dependency>
      <groupId>com.whiteelephant</groupId>
      <artifactId>monthandyearpicker</artifactId>
      <version>1.3.0</version>
      <type>pom</type>
    </dependency>
    compile 'com.whiteelephant:monthandyearpicker:1.3.0'
  2. Customize MonthPickerDialog styling via XML

    master

    By default, the picker uses the colorAccent from your application theme. To override the colors, define a custom style in your XML resources:

    <style name="MonthPickerDialogStyle" >
        <item name="monthBgColor">@color/bgColor</item>
        <item name="monthBgSelectedColor">@color/colorAccent</item>
        <item name="monthFontColorSelected">@color/selectionColor</item>
        <item name="monthFontColorNormal">@color/bgColor</item>
        <item name="monthFontColorDisabled">@color/bgColor</item>
    
        <item name="headerBgColor">@color/colorAccent</item>
        <item name="headerFontColorSelected">#fff</item>
        <item name="headerFontColorNormal">#85FFFFFF</item>
        <item name="headerTitleColor">#fff</item>
    
        <item name="dialogActionButtonColor">@color/colorAccent</item>
    </style>
  3. Initialize and show the MonthPickerDialog

    master

    Use the MonthPickerDialog.Builder to configure and display the picker. You must provide a context and an OnDateSetListener to handle the final selection of both month and year.

    MonthPickerDialog.Builder builder = new MonthPickerDialog.Builder(MainActivity.this, 
        new MonthPickerDialog.OnDateSetListener() {
          @Override
          public void onDateSet(int selectedMonth, int selectedYear) { 
            // Handle final month and year selection
          } 
        }, 
        today.get(Calendar.YEAR), 
        today.get(Calendar.MONTH));
    
    builder.setTitle("Select trading month")
           .setMinYear(1990)
           .setMaxYear(2030)
           .build()
           .show();
    MonthPickerDialog.Builder builder = new MonthPickerDialog.Builder(MainActivity.this, 
         new MonthPickerDialog.OnDateSetListener() {
           @Override
           public void onDateSet(int selectedMonth, int selectedYear) { // on date set }
         }, today.get(Calendar.YEAR), today.get(Calendar.MONTH));
        
        builder.setActivatedMonth(Calendar.JULY)
               .setMinYear(1990)
               .setActivatedYear(2017)
               .setMaxYear(2030)
               .setMinMonth(Calendar.FEBRUARY)
               .setTitle("Select trading month")
               .setMonthRange(Calendar.FEBRUARY, Calendar.NOVEMBER)
               .setOnMonthChangedListener(new MonthPickerDialog.OnMonthChangedListener() {
                 @Override
                 public void onMonthChanged(int selectedMonth) { // on month selected }})
               .setOnYearChangedListener(new MonthPickerDialog.OnYearChangedListener() {
                  @Override
                  public void onYearChanged(int selectedYear) { // on year selected }})
                .build()
                .show();
  4. Configure MonthPickerDialog selection ranges and modes

    master

    The MonthPickerDialog.Builder provides several methods to control the selection behavior:

    Range Constraints:

    • setMonthRange(int minMonth, int maxMonth): Sets both minimum and maximum month selections.
    • setYearRange(int minYear, int maxYear): Sets both minimum and maximum year selections.
    • setMonthAndYearRange(int minMonth, int maxMonth, int minYear, int maxYear): Sets month and year min/max values simultaneously.
    • setMinMonth(int minMonth) / setMaxMonth(int maxMonth): Sets individual month boundaries.
    • setMinYear(int minYear) / setMaxYear(int maxYear): Sets individual year boundaries.

    Display Modes:

    • showMonthOnly(): Displays only the month selection UI.
    • showYearOnly(): Displays only the year selection UI.

    Other Configuration:

    • setActivatedMonth(int activatedMonth): Sets the month that is selected when the picker opens.
    • setActivatedYear(int activatedYear): Sets the year that is selected when the picker opens.
    • setTitle(String title): Sets the title for the dialog. The title is hidden by default and only becomes visible if a value is set.
  5. Handle selection events with Listeners

    master

    You can listen for changes as the user navigates through months or years using the following listeners:

    • setOnMonthChangedListener(MonthPickerDialog.OnMonthChangedListener onMonthChange): Triggered when a new month is selected.
    • setOnYearChangedListener(MonthPickerDialog.OnYearChangedListener onYearChange): Triggered when a new year is selected.
    • OnDateSetListener: (Passed to the Builder constructor) Triggered when the final date (month and year) is confirmed.

    Example of implementing change listeners:

    builder.setOnMonthChangedListener(new MonthPickerDialog.OnMonthChangedListener() {
        @Override
        public void onMonthChanged(int selectedMonth) {
            // Logic for month change
        }
    });
  6. Configure MonthPickerView styling via XML attributes

    master

    The MonthPickerView can be customized using XML attributes defined under the monthPickerDialog styleable. These attributes allow you to control the colors of the header, months, and action buttons.

    Available attributes include:

    • headerBgColor: Background color of the header.
    • headerFontColorNormal: Font color for the header in normal state.
    • headerFontColorSelected: Font color for the header when selected.
    • monthBgColor: Background color of the month picker area.
    • monthBgSelectedColor: Background color of a selected month.
    • monthFontColorNormal: Font color for months in normal state.
    • monthFontColorSelected: Font color for the selected month.
    • monthFontColorDisabled: Font color for disabled months.
    • headerTitleColor: Color of the dialog title.
    • dialogActionButtonColor: Color for the 'OK' and 'Cancel' action buttons.
  7. Customize YearPickerView colors

    master

    You can pass a HashMap<String, Integer> to setColors(HashMap<String, Integer> colors) to customize the text colors of the year labels. The adapter looks for the following specific keys:

    • monthBgSelectedColor: The text color used when a year is currently selected.
    • monthFontColorNormal: The text color used for unselected years.
    HashMap<String, Integer> colors = new HashMap<>();
    colors.put("monthBgSelectedColor", Color.BLUE);
    colors.put("monthFontColorNormal", Color.BLACK);
    yearPickerView.setColors(colors);
  8. Use YearPickerView to select years

    master

    The YearPickerView is an Android UI component (extending ListView) used to display and select years. You can configure the range of available years, set the currently selected year, and listen for user selection changes.

    To handle user interactions, implement the OnYearSelectedListener interface and register it using setOnYearSelectedListener.

    yearPickerView.setOnYearSelectedListener(new YearPickerView.OnYearSelectedListener() {
        @Override
        public void onYearChanged(YearPickerView view, int year) {
            // Handle the year change
        }
    });
  9. Set month and year ranges in MonthPickerView

    master

    You can restrict the selectable range of months and years using the following methods:

    • setMonthRange(int minMonth, int maxMonth): Sets the minimum and maximum month. Months must be between Calendar.JANUARY and Calendar.DECEMBER.
    • setYearRange(int minYear, int maxYear): Sets the minimum and maximum year.
    • setMonthYearRange(int minMonth, int maxMonth, int minYear, int maxYear): Sets both month and year ranges simultaneously.

    Note: setMonthRange internally calls setMaxYear (likely a typo in the source for setMaxMonth), and setYearRange calls setMaxYear.