Android - Hide date field in datepickerdialog

24,042

Solution 1

Private DatePickerDialog createDialogWithoutDateField(){

    DatePickerDialog dpd = new DatePickerDialog(_activity, ExpiryDateSetListener,cyear,cmonth, cday);
    try{
    Field[] datePickerDialogFields = dpd.getClass().getDeclaredFields();
    for (Field datePickerDialogField : datePickerDialogFields) { 
        if (datePickerDialogField.getName().equals("mDatePicker")) {
            datePickerDialogField.setAccessible(true);
            DatePicker datePicker = (DatePicker) datePickerDialogField.get(dpd);
            Field datePickerFields[] = datePickerDialogField.getType().getDeclaredFields();
            for (Field datePickerField : datePickerFields) {
               if ("mDayPicker".equals(datePickerField.getName())) {
                  datePickerField.setAccessible(true);
                  Object dayPicker = new Object();
                  dayPicker = datePickerField.get(datePicker);
                  ((View) dayPicker).setVisibility(View.GONE);
               }
            }
         }

      }
    }catch(Exception ex){
    }
  return dpd;

}

Use the above code for hiding the day field in DatePickerDialog.

Refer this LINK

Solution 2

I don't recommend using Reflection to do this kind of thing.

There is a simpler and more pretty way to do so:

((ViewGroup) datePickerDialog.getDatePicker()).findViewById(Resources.getSystem().getIdentifier("day", "id", "android")).setVisibility(View.GONE);

Be aware that .getDatePicker() method from DatePickerDialog works on API LEVEL >= 11.

Solution 3

The source code to DatePicker and DatePickerDialog are available for you to clone, refactor into your own package, and modify to suit.

Solution 4

((ViewGroup) datePickerDialog.getDatePicker()).findViewById(Resources.getSystem().getIdentifier("day", "id", "android")).setVisibility(View.GONE); 

This works properly

Solution 5

    private Calendar mCalendar;
    mCalendar = Calendar.getInstance();// Initialize Calendar

dialog_date_picker.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/rounded_corners"
android:orientation="vertical"
android:padding="8dp">
<DatePicker
    android:id="@+id/date_picker"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="4"
    android:calendarViewShown="false"
    android:datePickerMode="spinner" />
<Button
    android:id="@+id/date_time_set"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="@color/colorPrimary"
    android:text="Set"
    android:textColor="@color/colorWhite" /></LinearLayout>

DialogMethod called on Click:

private void ExpiryDialog() {
    System.out.println("Inside Dialog Box");
    final Dialog dialog = new Dialog(context);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.dialog_date_picker);
    dialog.show();
    final DatePicker datePicker = (DatePicker) dialog.findViewById(R.id.date_picker);
    date_time_set = (Button) dialog.findViewById(R.id.date_time_set);
    datePicker.init(mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH), null);

    LinearLayout ll = (LinearLayout) datePicker.getChildAt(0);
    LinearLayout ll2 = (LinearLayout) ll.getChildAt(0);
    ll2.getChildAt(1).setVisibility(View.INVISIBLE);

    date_time_set.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view){
            dialog.dismiss();
            Integer month = datePicker.getMonth()+1;
            Integer year = datePicker.getYear();
            expMonth = (month.toString().length()   == 1 ? "0"+month.toString():month.toString());
            expYear = year.toString();
            etExpiryDate.setText(expMonth + "/" + expYear);
        }
    });
}

This code works and hides the day in a DatePicker, so I can show Expiry Date for Cards. Enjoy!!

Share:
24,042
Ishu
Author by

Ishu

Updated on July 17, 2022

Comments

  • Ishu
    Ishu almost 2 years

    How to hide/remove date field in datepickerdialog. Actually my requirement is to use the native date picker dialog but it shows up with day, month, and year. But as per my requirement i need only month and year. So i want to hide or remove the day field from the view.

    Any help will be much usefull.

  • CommonsWare
    CommonsWare about 12 years
    This is a vile recommendation. The most reliable answer is to roll your own. Even if you want to play script-kiddie games like what is proposed here, don't use reflection -- get at the particular widgets via getDatePicker() and findViewById() and set their visibility that way. Both approaches make assumptions about internal implementations that may not be valid across Android OS releases or OEM firmware modifications, but at least the findViewById() approach will make fewer assumptions (and probably run faster).
  • Tancho
    Tancho almost 11 years
    mDatePicker.getDatePicker().setCalendarViewShown(false);
  • Karthik Andhamil
    Karthik Andhamil over 10 years
    Awesome. But "mDayPicker" does not work. It should be "mDaySpinner".
  • Ryan C
    Ryan C over 9 years
    This will cause a null pointer exception on lollipop devices as they use a different date picker layout.
  • darthShadow
    darthShadow over 7 years
    Some more comments regarding what the code does would be helpful.
  • SH7890
    SH7890 over 6 years
    I'm also getting a Null Pointer Exception. I'm on Android 6.0.1
  • SH7890
    SH7890 over 6 years
    On Android 6.0.1, I get a NullPointerException.
  • Tancho
    Tancho over 4 years
    this comment is from 2013 :) maybe the public api changed and the call is deprecated ?
  • Mahmoud Mabrok
    Mahmoud Mabrok almost 4 years
    give me null view