-1

I'm trying to show only day of month and month fields in DatePicker in AlertDialog. On previous API's from API 21, i can do it with code below.But when i try to do it in API21+ "getDeclaredFields()" doesn't even returns like "mYearPicker" or "mYearSpinner" fields.Does anyone have any idea how can i just show Day and Month fields?And i don't even know what field "116" is.

My code like this:

 final DatePicker datePickerStart = new DatePicker(new ContextThemeWrapper(getActivity(), android.R.style.Theme_Holo_Light_Dialog_NoActionBar));
    datePickerStart .setCalendarViewShown(false);
    datePickerStart .setSpinnersShown(true);
    try {
        Field f[] = datePickerStart .getClass().getDeclaredFields();
        for (Field field : f) {
            if (field.getName().equals("mYearPicker")|| field.getName().equals("mYearSpinner")) {
                field.setAccessible(true);
                Object yearPicker = new Object();
                yearPicker = field.get(datePickerStart);
                ((View) yearPicker).setVisibility(View.GONE);
            }
        }
    }
    catch (Exception e) {
        Log.e("ERROR", e.getMessage());
    }

Should look like: Before API 21

Looks like this: API 21+

Any idea?

Goktug
  • 15
  • 1
  • 5
  • Um, why not fork `DatePicker` and modify it to suit? Or, why not use [a third-party library](http://android-arsenal.com/tag/27)? What makes you think that this approach will work across thousands of Android device models, given that device manufacturers can change Android internals, let alone Google with new Android versions? – CommonsWare May 10 '16 at 17:15
  • seems like a duplicate of http://stackoverflow.com/questions/30789907/hide-day-month-or-year-from-datepicker-in-android-5-0-lollipop – Morse May 10 '16 at 17:17
  • @Morse thanks!! it worked well – Goktug May 10 '16 at 17:36

1 Answers1

-1

Thanks to @Morse i found the solution HERE

public DatePicker initMonthPicker(){
    DatePicker dp_mes = new DatePicker(new ContextThemeWrapper(getActivity(), android.R.style.Theme_Holo_Light_Dialog_NoActionBar));

    int year    = dp_mes.getYear();
    int month   = dp_mes.getMonth();
    int day     = dp_mes.getDayOfMonth();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
        int daySpinnerId = Resources.getSystem().getIdentifier("day", "id", "android");
        if (daySpinnerId != 0)
        {
            View daySpinner = dp_mes.findViewById(daySpinnerId);
            if (daySpinner != null)
            {
                daySpinner.setVisibility(View.VISIBLE);
            }
        }

        int monthSpinnerId = Resources.getSystem().getIdentifier("month", "id", "android");
        if (monthSpinnerId != 0)
        {
            View monthSpinner = dp_mes.findViewById(monthSpinnerId);
            if (monthSpinner != null)
            {
                monthSpinner.setVisibility(View.VISIBLE);
            }
        }

        int yearSpinnerId = Resources.getSystem().getIdentifier("year", "id", "android");
        if (yearSpinnerId != 0)
        {
            View yearSpinner = dp_mes.findViewById(yearSpinnerId);
            if (yearSpinner != null)
            {
                yearSpinner.setVisibility(View.GONE);
            }
        }
    } else { //Older SDK versions
        Field f[] = dp_mes.getClass().getDeclaredFields();
        for (Field field : f)
        {
            if(field.getName().equals("mDayPicker") || field.getName().equals("mDaySpinner"))
            {
                field.setAccessible(true);
                Object dayPicker = null;
                try {
                    dayPicker = field.get(dp_mes);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                ((View) dayPicker).setVisibility(View.GONE);
            }

            if(field.getName().equals("mMonthPicker") || field.getName().equals("mMonthSpinner"))
            {
                field.setAccessible(true);
                Object monthPicker = null;
                try {
                    monthPicker = field.get(dp_mes);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                ((View) monthPicker).setVisibility(View.VISIBLE);
            }

            if(field.getName().equals("mYearPicker") || field.getName().equals("mYearSpinner"))
            {
                field.setAccessible(true);
                Object yearPicker = null;
                try {
                    yearPicker = field.get(dp_mes);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                ((View) yearPicker).setVisibility(View.GONE);
            }
        }
    }
    return dp_mes;
}

So i just call this method when i need for a new DatePicker object

Community
  • 1
  • 1
Goktug
  • 15
  • 1
  • 5