Hide Date from Date Picker

11,246

Solution 1

After Brain Storming half a day i have found this solution this may help other having such scenarios

My Custom class for date Picker is Following that will update the date in title every time user change month/year

class CustomDatePickerDialog extends DatePickerDialog implements OnDateChangedListener {

private DatePickerDialog mDatePicker;

@SuppressLint("NewApi")
public CustomDatePickerDialog(Context context,int theme, OnDateSetListener callBack,
        int year, int monthOfYear, int dayOfMonth) {
    super(context, theme,callBack, year, monthOfYear, dayOfMonth);
    mDatePicker = new DatePickerDialog(context,theme,callBack, year, monthOfYear, dayOfMonth);

    mDatePicker.getDatePicker().init(2013, 7, 16, this);

    updateTitle(year, monthOfYear);

}
public void onDateChanged(DatePicker view, int year,
        int month, int day) {
    updateTitle(year, month);
}
private void updateTitle(int year, int month) {
    Calendar mCalendar = Calendar.getInstance();
    mCalendar.set(Calendar.YEAR, year);
    mCalendar.set(Calendar.MONTH, month);
//       mCalendar.set(Calendar.DAY_OF_MONTH, day);
        mDatePicker.setTitle(getFormat().format(mCalendar.getTime()));

}   

public DatePickerDialog getPicker(){

    return this.mDatePicker;
}
    /*
     * the format for dialog tile,and you can override this method
     */
public SimpleDateFormat getFormat(){
    return new SimpleDateFormat("MMM, yyyy");
};   
}

and the following is the code that will hide the day and its spinner from the date picker dialog

CustomDatePickerDialog dp = new CustomDatePickerDialog(context, android.R.style.Theme_Holo_Light_Dialog,  datePickerListener, year, month, day);

               DatePickerDialog obj = dp.getPicker();
              try{
                         Field[] datePickerDialogFields = obj.getClass().getDeclaredFields();
                         for (Field datePickerDialogField : datePickerDialogFields) { 
                             if (datePickerDialogField.getName().equals("mDatePicker")) {
                                 datePickerDialogField.setAccessible(true);
                                 DatePicker datePicker = (DatePicker) datePickerDialogField.get(obj);
                                 Field datePickerFields[] = datePickerDialogField.getType().getDeclaredFields();
                                 for (Field datePickerField : datePickerFields) {
                                    if ("mDayPicker".equals(datePickerField.getName()) || "mDaySpinner".equals(datePickerField
                                      .getName())) {
                                       datePickerField.setAccessible(true);
                                       Object dayPicker = new Object();
                                       dayPicker = datePickerField.get(datePicker);
                                       ((View) dayPicker).setVisibility(View.GONE);
                                    }
                                 }
                              }

                           }
                         }catch(Exception ex){
                         }
              obj.show();

Solution 2

Simply way - just to hide NumberPicker in datePickerDialog here is code from my project

public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        final Calendar c = Calendar.getInstance();

        DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, c.get(Calendar.YEAR),
                c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));

        dialog.getDatePicker().setSpinnersShown(true);

        // hiding calendarview and daySpinner in datePicker
            dialog.getDatePicker().setCalendarViewShown(false);

            LinearLayout pickerParentLayout = (LinearLayout) dialog.getDatePicker().getChildAt(0);

            LinearLayout pickerSpinnersHolder = (LinearLayout) pickerParentLayout.getChildAt(0);

            pickerSpinnersHolder.getChildAt(0).setVisibility(View.GONE);


        dialog.setTitle("Pick a date");
        return dialog;
    }

    public void onDateSet(DatePicker view, int yy, int mm, int dd) {

        Calendar result = Calendar.getInstance();

        result.set(Calendar.YEAR, yy);
        result.set(Calendar.MONDAY, mm);
        result.set(Calendar.DAY_OF_MONTH, dd);

        if (currentWeek != -1) {
            updateDateWeek(result.getTimeInMillis());
        } else {
            updateDate(result.getTimeInMillis());
        }
    }
}

to show picker just call

DialogFragment newFragment = new SelectDateFragment();
newFragment.show(getActivity().getSupportFragmentManager(), "DatePicker");

Solution 3

This might work for you.. if you are doing for only English.. also can be changes according to the requirements.

Locale.setDefault(Locale.UK);

This will make date picker to appear in the form DD/MM/YYYY

Following code removes day spiner

LinearLayout pickerParentLayout =
(LinearLayout) mDatePicker.getChildAt(0);
LinearLayout pickerSpinnersHolder = (LinearLayout) pickerParentLayout.getChildAt(0);
NumberPicker picker = (NumberPicker) pickerSpinnersHolder.getChildAt(0);
picker.setVisibility(View.GONE);

Solution 4

This worked for me after trying out several different approaches.

 datePicker = (DatePicker) findViewById(R.id.date_picker);

 public void hideDay() {
    try {
        java.lang.reflect.Field[] f = datePicker.getClass().getDeclaredFields();
        for (java.lang.reflect.Field field : f) {
            if (field.getName().equals("mDayPicker") || field.getName().equals("mDaySpinner")) {
                field.setAccessible(true);
                Object dmPicker = new Object();
                dmPicker = field.get(datePicker);
                ((View) dmPicker).setVisibility(View.GONE);
            }
        }
    }
    catch (SecurityException e) {
        TLog.d("ERROR", e.getMessage());
    }
    catch (IllegalArgumentException e) {
        TLog.d("ERROR", e.getMessage());
    }
    catch (IllegalAccessException e) {
        TLog.d("ERROR", e.getMessage());
    }
}
Share:
11,246

Related videos on Youtube

Usman Kurd
Author by

Usman Kurd

Android Developer

Updated on June 04, 2022

Comments

  • Usman Kurd
    Usman Kurd almost 2 years

    Currently I am using date picker native one but i want to change it in such a way that i only needed month and year how to modify this my code is following

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
           // set date picker as current date
           return new DatePickerDialog(this, datePickerListener, 
                         year, month,day);
        }
        return null;
    }
    
    private DatePickerDialog.OnDateSetListener datePickerListener
                = new DatePickerDialog.OnDateSetListener() {
    
        // when dialog box is closed, below method will be called.
        public void onDateSet(DatePicker view, int selectedYear,
                int selectedMonth, int selectedDay) {
    
        /*  year = selectedYear;
            month = selectedMonth;
            day = selectedDay;*/
            mynewYear=selectedYear;
            myNewMonth=selectedMonth;
            myNewDay=selectedDay;
            try {
                checkDate();
            } catch (NoSuchAlgorithmException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            /*time_date_TV.setText(new StringBuilder()
              .append(myNewMonth + 1).append("/")
              .append(myNewDay ).append("/").append(mynewYear));
                   */
    
    
    
    
    
        }
    };
    

    I just want to hide date Section any help will be Thank full

  • Aleks G
    Aleks G over 10 years
    Be very careful using this method. You are effectively tying your code to specific implementation of the DatePickerDialog. If in the next minor version Google changes its implementation (e.g. renames the variable mDatePicker), your code will break. Moreover, if it works on stock google android, it doesn't mean that it will work on other devices, because different manufacturers often change things about in their implementation. I found it the hard way when using a similar technique.
  • Usman Kurd
    Usman Kurd over 10 years
    agreed with you will optimize it further
  • Heitara
    Heitara about 10 years
    I would suggest pickerSpinnersHolder.findViewById(R.id.day).setVisibility(Vi‌​ew.GONE); based on mDaySpinner = (NumberPicker) findViewById(R.id.day); from android source code, because when your locate is US, this code hides the month spinner.
  • Omama
    Omama over 8 years
    Agreed with Aleks G. He is absolutely right, this customization wont work for OS 5.0 or greater!!!
  • APP
    APP almost 4 years
    AlertDialog.THEME_HOLO_DARK is deprecated