Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Date selector with only Day and Month

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?

like image 759
checklist Avatar asked Feb 17 '14 20:02

checklist


People also ask

How can I enable only specific days in DatePicker Android?

To make only dates in the past selectable using the DatePicker set the maximum date on the DatePicker to the current date. This will prevent you from selecting the current day and days in the future. If you want to allow the current day to be selected as well, set the maximum date to tomorrow's date.

What is date and time picker in Android?

Android provides controls for the user to pick a time or pick a date as ready-to-use dialogs. Each picker provides controls for selecting each part of the time (hour, minute, AM/PM) or date (month, day, year).


1 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;
like image 173
Simon Avatar answered Oct 13 '22 00:10

Simon