5

Im working on app that needs to input expiration date for credit card but andorid 5.0+ date time picker has only options for picking date with days. Is there any library for that because I cant find any. I also didnt find anything in material design guidelines regarding this.

Thx

Dominik Mičuta
  • 357
  • 4
  • 13

2 Answers2

1

Went ahead making a modified Date Picker called Simple Date picker ... have used the code similar to Date Picker just to show month and year. You can also provide your own theme to the SimpleDatePickerDialog.

see https://github.com/resengupta/Month-Year-Date-Picker

ReeSen
  • 75
  • 4
0

Please try this

        Field f[] = picker.getClass().getDeclaredFields();
    for (Field field : f) {
        if (field.getName().equals("mDayPicker")) {
            field.setAccessible(true);
            Object dayPicker = new Object();
            dayPicker = field.get(picker);
            ((View) dayPicker).setVisibility(View.GONE);
        }
    }
} 
catch (SecurityException e) {
    Log.d("ERROR", e.getMessage());
} 
catch (IllegalArgumentException e) {
    Log.d("ERROR", e.getMessage());
} 
catch (IllegalAccessException e) {
    Log.d("ERROR", e.getMessage());
}

you can try this

DatePicker dpDate = (DatePicker) findViewById(R.id.dpDate);

// Initialize Date Picker
int year    = dpDate.getYear();
int month   = dpDate.getMonth();
int day     = dpDate.getDayOfMonth();
dpDate.init(year, month, day, this);

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

        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) 
        {
            int monthSpinnerId = Resources.getSystem().getIdentifier("month", "id", "android");
            if (monthSpinnerId != 0) 
            {
                View monthSpinner = dpDate.findViewById(monthSpinnerId);
                if (monthSpinner != null) 
                {
                    monthSpinner.setVisibility(View.GONE);
                }
            }
        }

        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) 
        {
            int yearSpinnerId = Resources.getSystem().getIdentifier("year", "id", "android");
            if (yearSpinnerId != 0) 
            {
                View yearSpinner = dpDate.findViewById(yearSpinnerId);
                if (yearSpinner != null) 
                {
                    yearSpinner.setVisibility(View.GONE);
                }
            }
        }

for More Information

Hide Day, Month, or Year from DatePicker in Android 5.0+ Lollipop

Community
  • 1
  • 1
Ashish Agrawal
  • 1,977
  • 2
  • 18
  • 32