Search code examples
androiddatepickerdialog

android DatePickerDialog display only one button


I know there are a lot of problems with DatePickerDialog as explain at this post Jelly Bean DatePickerDialog --- is there a way to cancel? but my problem is that I want to only display the confirmation button instead of two buttons. I use a DatePickerDialog in DialogFragment like this:

public class DateDialog extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current date as the default date in the picker
    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);

    // set up mindate
    minYear = year;
    minMonth = month;
    minDay = day;

    // Create a new custom instance of DatePickerDialog and return it
    return new CustomDatePickerDialog(getActivity(), this, year, month, day);
}
}

Solution

  • I've finally found an easier solution. Here is the workaraound in my CustomDatePickerDialog which extends DatePickerDialog

    public CustomDatePickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear,int dayOfMonth) {
        super(context, callBack, year, monthOfYear, dayOfMonth);
        this.setButton(DatePickerDialog.BUTTON_POSITIVE, "OK",this);
        this.setButton(DatePickerDialog.BUTTON_NEGATIVE, "",this);  // hide cancel button
    
    }