Usman Kurd
Usman Kurd

Reputation: 7023

Hide Date from Date Picker

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

Upvotes: 2

Views: 11941

Answers (6)

himanshu
himanshu

Reputation: 1

http://learnlinky.com/2016/12/disable-day-selection-android-datepicker/

This worked for me :)

DatePickerDialog datePickerDialog = 
    new DatePickerDialog(getActivity(), AlertDialog.THEME_HOLO_DARK,
    new DatePickerDialog.OnDateSetListener() { 
        @Override
        public void onDateSet(DatePicker view, 
                              int year,
                              int monthOfYear, 
                              int dayOfMonth) { 
            txtMonth.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year); 
        }
    }, mYear, mMonth, mDay);
        ((ViewGroup)datePickerDialog.getDatePicker())
             .findViewById(Resources.getSystem().getIdentifier("day", "id", "android"))
             .setVisibility(View.GONE);
datePickerDialog.show();

Upvotes: 0

Alexiscanny
Alexiscanny

Reputation: 579

I modified few things like: Made style the android default Theme_Holo_Dialog and delete the day from the visibility.

Following few changes my code is:

pick_expire_date.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Get Current Date
            final Calendar c = Calendar.getInstance();
            cyear = c.get(Calendar.YEAR);
            cmonth = c.get(Calendar.MONTH);
            cday = c.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog datePickerDialog = new DatePickerDialog(
                    CheckoutPaymentActivity.this,
                    android.R.style.Theme_Holo_Dialog,
                    new DatePickerDialog.OnDateSetListener() {
                        @Override
                        public void onDateSet(DatePicker view, int year,
                                              int monthOfYear, int dayOfMonth) {
                            pick_expire_date.setText((monthOfYear + 1)+"/"+year);
                            mMonth = monthOfYear + 1;
                            mYear = year;
                        }
                    },
                    cyear, cmonth, cday);
            datePickerDialog.getDatePicker().findViewById(getResources().getIdentifier("day","id","android")).setVisibility(View.GONE);
            datePickerDialog.show();
        }
    });

Upvotes: 0

Wahib Ul Haq
Wahib Ul Haq

Reputation: 4385

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());
    }
}

Upvotes: 1

Kantesh
Kantesh

Reputation: 333

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);

Upvotes: 2

Andrew F
Andrew F

Reputation: 1711

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");

Upvotes: 2

Usman Kurd
Usman Kurd

Reputation: 7023

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();

Upvotes: 2

Related Questions