Material Design datepickerdialog

16,355

Solution 1

https://github.com/wdullaer/MaterialDateTimePicker

a good Material Date Picker Library. Easy to use.

Just add compile'com.wdullaer:materialdatetimepicker:1.4.2' to your app: build.gradle

for example: Date Picker Dialog

// Show a datepicker when the dateButton is clicked
    dateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar now = Calendar.getInstance();
                DatePickerDialog dpd = DatePickerDialog.newInstance(
                        MainActivity.this,
                        now.get(Calendar.YEAR),
                        now.get(Calendar.MONTH),
                        now.get(Calendar.DAY_OF_MONTH)
                );
                dpd.setThemeDark(modeDarkDate.isChecked());
                dpd.vibrate(vibrateDate.isChecked());
                dpd.show(getFragmentManager(), "Datepickerdialog");
            }
        });

and get your picked date from its implements method 'onDateSet' function

@Override
    public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
        String date = "You picked the following date: "+dayOfMonth+"/"+(++monthOfYear)+"/"+year;
        dateTextView.setText(date);
    }

Solution 2

You cannot have material design pickers in lower versions, the support library does not have these widgets. You will have to use 3rd party libraries. You can try this library, the author took the DateTimePicker from Android Framework which you see in Android Lollipop and compiled into a library and it supports from Android 4.0 on wards.

To add it in your app just add below lines in build.gradle:

dependencies {
  compile 'com.wdullaer:materialdatetimepicker:1.4.1'
}

Find it on github link

The code taken from Android Framework can be found here

Share:
16,355

Related videos on Youtube

user3278732
Author by

user3278732

Updated on September 16, 2022

Comments

  • user3278732
    user3278732 over 1 year

    I would like to know if I can implement material design datepickerdialog for my android app

    Code:

        import java.util.Calendar;
        import android.app.DatePickerDialog;
        import android.app.Dialog;
        import android.os.Bundle;
        import android.support.v4.app.DialogFragment;
        import android.widget.DatePicker;
        import android.widget.EditText;
        public class SelectDateFragment  extends DialogFragment implements
        DatePickerDialog.OnDateSetListener {
        EditText mEdit;
        public Dialog onCreateDialog(Bundle savedInstanceState) {
              final Calendar calendar = Calendar.getInstance();
              int yy = calendar.get(Calendar.YEAR);
              int mm = calendar.get(Calendar.MONTH);
              int dd = calendar.get(Calendar.DAY_OF_MONTH);
              return   new DatePickerDialog(getActivity(), this, yy, mm, dd);
        }
        @Override
        public void onDateSet(DatePicker view, int yy, int mm, int dd) {
             populateSetDate(yy, mm + 1, dd);
        }
        public void populateSetDate(int year, int month, int day) {
             mEdit = (EditText) getActivity().findViewById(R.id.date);
             mEdit.setText(day + "/" + month + "/" + year);
        }
        }
    

    Simply, I dont want to use third part libraries.I just want to know if it can be done using android support libraries and how.

  • Asha
    Asha over 7 years
    Don't forget to implements the DatePickerDialog.OnDateSetListener with your activity. Because the first argument in DatePickerDialog newInstance method refer OnDateSetListener.
  • CoolMind
    CoolMind over 7 years
    Don't forget to assign listeners to an activity or a fragment on device rotation like here: android-arsenal.com/details/1/3779 and turn off vibration and permission: github.com/wdullaer/MaterialDateTimePicker/issues/55.
  • Mohamed Haseel
    Mohamed Haseel over 5 years
    Can you cite where does Google recommend this?