Search code examples
androiddatepickerdialog

I want to change DatePickerDialog


I'm an android developer. I want to use DatePickerDialog. But this widget is showing Year - Month. I want to show Month - Year. How?


Solution

  • Try this -

    ((ViewGroup) datePickerDialog.getDatePicker()).findViewById(Resources.getSystem().getIdentifier("day", "id", "android")).setVisibility(View.GONE);
    

    It works on >= 11

    If you need to customise your picker as per your requirement then better to create your own using NumberPickers, Here is the example -

    public class MonthYearPickerDialog extends DialogFragment {
    
      private static final int MAX_YEAR = 2099;
      private DatePickerDialog.OnDateSetListener listener;
    
      public void setListener(DatePickerDialog.OnDateSetListener listener) {
        this.listener = listener;
      }
    
      @Override
      public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();
    
        Calendar cal = Calendar.getInstance();
    
        View dialog = inflater.inflate(R.layout.date_picker_dialog, null);
        final NumberPicker monthPicker = (NumberPicker) dialog.findViewById(R.id.picker_month);
        final NumberPicker yearPicker = (NumberPicker) dialog.findViewById(R.id.picker_year);
    
        monthPicker.setMinValue(1);
        monthPicker.setMaxValue(12);
        monthPicker.setValue(cal.get(Calendar.MONTH) + 1);
    
        int year = cal.get(Calendar.YEAR);
        yearPicker.setMinValue(year);
        yearPicker.setMaxValue(MAX_YEAR);
        yearPicker.setValue(year);
    
        builder.setView(dialog)
            // Add action buttons
            .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int id) {
                listener.onDateSet(null, yearPicker.getValue(), monthPicker.getValue(), 0);
              }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int id) {
                MonthYearPickerDialog.this.getDialog().cancel();
              }
            });
        return builder.create();
      }
    }
    

    Layout -

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:orientation="vertical">
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="horizontal">
    
            <NumberPicker
                android:id="@+id/picker_year"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="20dp"
                android:layout_marginRight="20dp">
    
            </NumberPicker>
    
            <NumberPicker
                android:id="@+id/picker_month"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
    
            </NumberPicker>
    
        </LinearLayout>
    </LinearLayout>
    

    To show the dialogue box -

    MonthYearPickerDialog pd = new MonthYearPickerDialog();
    pd.setListener(this);
    pd.show(getFragmentManager(), "MonthYearPickerDialog");
    

    Reference - https://stackoverflow.com/a/31653048/1741586

    Hope it will work :)