Home » Android » datepicker – Android Date selector with only Day and Month

datepicker – Android Date selector with only Day and Month

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am looking for a way to customize the Date selector in Android to only show the day and month (i.e. no year).

I am creating a dialog based on How to display date picker for android with only month and year fields?:

    Dialog dlg = new DatePickerDialog(context, datePickerListener, dueDateCalendar.get(Calendar.YEAR), dueDateCalendar.get(Calendar.MONTH), dueDateCalendar.get(Calendar.DAY_OF_MONTH));
    try {
        Field f[] = dlg.getClass().getDeclaredFields();
        for (Field field : f) {
            String name = field.getName();
            if (name.equals("YEAR")){
                field.setAccessible(true);
                Object dayPicker = new Object();
                dayPicker = field.get(dlg);
                ((View) dayPicker).setVisibility(View.GONE);
            }
        }
    } catch (Exception e){
        // TODO: should not happen
        e.printStackTrace();
    }
    return dlg;

But I keep getting a Cast exception on ((View) dayPicker).setVisibility(View.GONE);

java.lang.ClassCastException: java.lang.String cannot be cast to
android.view.View

Any ideas?

How to&Answers:

Here give this a go. Its APIv11+ but there are other ways on lower API versions.

DatePickerDialog dlg = new DatePickerDialog(context, datePickerListener, 
    dueDateCalendar.get(Calendar.YEAR), 
    dueDateCalendar.get(Calendar.MONTH), 
    dueDateCalendar.get(Calendar.DAY_OF_MONTH));
int year = context.getResources().getIdentifier("android:id/year", null, null);
if(year != 0){
    View yearPicker = dlg.getDatePicker().findViewById(year);
    if(yearPicker != null){
        yearPicker.setVisibility(View.GONE);
    }
}
return dlg;

Updated code: This should do the job.

DatePickerDialog dlg = new DatePickerDialog(context, datePickerListener, 
    dueDateCalendar.get(Calendar.YEAR), 
    dueDateCalendar.get(Calendar.MONTH), 
    dueDateCalendar.get(Calendar.DAY_OF_MONTH))
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        int year = getContext().getResources()
            .getIdentifier("android:id/year", null, null);
        if(year != 0){
            View yearPicker = findViewById(year);
            if(yearPicker != null){
                yearPicker.setVisibility(View.GONE);
            }
        }
    }
};
return dlg;

Answer:

This is Works for me :

DatePickerDialog monthDatePickerDialog = new DatePickerDialog(activity, 
AlertDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        monthTextView.setText(dayOfMonth+ "/" + (month + 1));
    }
}, yearNow, monthNow, dayNow){
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getDatePicker().findViewById(getResources().getIdentifier("year","id","android")).setVisibility(View.GONE);
    }
};
monthDatePickerDialog.setTitle("select_month");
monthDatePickerDialog.show();