How to highlight dates in android datepicker?

Viewed 840

How to highlight dates in Android datepicker like is on the picture below? On the internet, I founded few solutions but they don't use "original" Android datepicker. Is there some solution where I can edit Android datepicker, that I am already using?

Datepicker with highlighted dates

2 Answers

The official DatePicker Android Component, does not support this feature, that's why everyone is creating it's custom view or using a library to do so.

As @ADM said, there's also the MaterialDatePicker which contains more functionality than the old one, you can check this material design post to get an idea of what you can do.

And for more information MaterialDatePicker documentation.

Edit

To disable dates you should use MaterialDatePicker - CalendarConstraints.DateValidator, where you can set the dates you want to disable or enable. You don't have to create a custom class in this case but you should create your own implementation of this, you should override the isValid(long)method and there you choose the days you want to show as enabled or disabled.

An example of the code you should write is written on this answer : https://stackoverflow.com/a/61147318/4385913

Similar to this question - it's now possible to highlight the background of valid dates in Material DatePicker by extending the DayViewDecorator class.

There is an official implementation in the material catalog.

The very simplified version goes something like:

class BackgroundHighlightDecorator extends DayViewDecorator {

    private final List<Date> myDates = /* your valid dates */;

    private ColorStateList backgroundHighlightColor;

    @Override
    public void initialize(Context context) {
    
      int highlightColor = Color.BLUE;
      backgroundHighlightColor = ColorStateList.valueOf(highlightColor);

    }

    @Override
    public ColorStateList getBackgroundColor(Context context, int year, int month, int day, boolean valid, boolean selected) {
        return valid && !selected && shouldShowHighlight(year, month, day)
        ? backgroundHighlightColor
        : null;
     }

     private boolean shouldShowHighlight(int year, int month, int day) {
        if (/* your condition for validity */) {
                return true;
        } else {
      
            return false;
         }
    }



    /** CREATOR code goes here **/
}
Related