I want to change the date picker of my project to the date picker provided by the Material Components for Android, but it is not working.
This is the code I've tried:
MaterialDatePicker.Builder<Long> builder = MaterialDatePicker.Builder.datePicker();
MaterialDatePicker<Long> picker = builder.build();
picker.show(getSupportFragmentManager(), picker.toString());
This is how it looked like:
An this is how it should've looked like:
Can anybody tell me what's missing?
Thanks
Add latest dependency:
Other options: https://github.com/wdullaer/MaterialDateTimePicker
With the Material Components for Android you can use the new
MaterialDatePicker
.To work fine, in your app you have to use a Material Components Theme.
In this way you inherit the style and theme for the pickers.
To select a single date just use:
To select a range date you can use a DateRange Picker using:
Check the colors used in your theme.
These attributes define your style. You don't need to add them, they are provided by default with the Material Components theme.
Based on these style, the colors used by the picker are:
For getting your desired output, make sure to add this before calling
show()
:For Material DatePicker specifically, you need to first implement the latest library
Then
You can also simply show the DatePickerDialog instead of using Builder. The Dialog is For showing dialog and getting selected data:
The problem was in the colorPrimary.
The default color of my project to colorPrimary was "white" and the Material Date Picker style uses that colorPrimary to color the background and the text of the buttons. Since the color of the header text was also white, it appear that there was nothing there when there was everything.
I solved it by importing the styles file to my project and making some adjustments to the styles in my project.
Thank you all for your answers, all of them helped in finding the problem!