Android Date Picker Material Style

APPGIS picture APPGIS · Feb 14, 2017 · Viewed 17.2k times · Source

i have implemented the date picker on my fragment, this is the code:

edittext_from.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    final Calendar c = Calendar.getInstance();
                    mYear = c.get(Calendar.YEAR);
                    mMonth = c.get(Calendar.MONTH);
                    mDay = c.get(Calendar.DAY_OF_MONTH);
                    android.app.DatePickerDialog datePickerDialog = new android.app.DatePickerDialog(getContext(),
                            new android.app.DatePickerDialog.OnDateSetListener() {

                                @Override
                                public void onDateSet(DatePicker view, int year,
                                                      int monthOfYear, int dayOfMonth) {

                                    edittext_from.setText(String.format("%04d-%02d-%02d", year, (monthOfYear + 1), dayOfMonth));

                                }
                            }, mYear, mMonth, mDay);
                    datePickerDialog.show();
                }


            });

and the style of my date picker is a old style as this :DatePicker

I would like to use a DatePicker with material style, i have tried to use this library compile 'com.wdullaer:materialdatetimepicker:2.3.0', but the result is the same. Any help how i can change this DatePicker with Material Date Picker? Thanks

Answer

Gabriele Mariotti picture Gabriele Mariotti · Aug 20, 2019

With the Material Components for Android you can use the new MaterialDatePicker.

Currently it is under active development and requires version 1.1.0 of material components for android library.

implementation 'com.google.android.material:material:1.1.0'

Just use:

       MaterialDatePicker.Builder<Long> builder = MaterialDatePicker.Builder.datePicker();
       builder.setTitleText(R.string.your_text);
       MaterialDatePicker<Long> picker = builder.build();
       picker.show(getSupportFragmentManager(), picker.toString());

enter image description here