Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable specific dates of day in Android date picker

I'm using the datePicker and I can disable last days of today and later days after 30 days by the following code:

DatePickerDialog datePicker = new DatePickerDialog();

             Calendar calender = Calendar.getInstance();
             long today = calender.getTimeInMillis();
             final long oneDay = 24 * 60 * 60 * 1000L;

             Date previousDays = new Date(today - 1000);
             datePicker.setMinDate(DateToCalendar(previousDays));

             Date nextMonth = new Date(today + 30 * oneDay);
             datePicker.setMaxDate(DateToCalendar(nextMonth));

If I want to disable Friday of every month, how can I do this?

like image 682
Emy Alsabbagh Avatar asked Feb 24 '16 10:02

Emy Alsabbagh


People also ask

How do I turn off past dates?

the previous dates we need to set the minDate property of the date picker. if we set minDate:0 then it will disable all the previous dates. and we set input attribute min:current_date then it will disable all the previous dates.

How do I turn off the date in flutter?

In the Flutter date range picker, you can enable or disable the past dates by using the enablePastDates property of the calendar. When setting the enablePastDates property value as false, you can't able to select the past dates.


3 Answers

You can use this library Material Date Time Picker, here you can set an option to show specific dates, For Example:

datePicker.setSelectableDays(Calendar[] days)

And pass array of Calendar as an parameter which contains all the selectable date.

like image 107
himanshu1496 Avatar answered Sep 29 '22 16:09

himanshu1496


Use custom DatePickerDialog

add this in build.gradle

compile 'com.wdullaer:materialdatetimepicker:3.5.1'

implement

DatePickerDialog.OnDateSetListener to your class

and import

import com.wdullaer.materialdatetimepicker.date.DatePickerDialog; instead of 'import android.app.DatePickerDialog;'

and add this method

 private void showDatePicker() { 

    Calendar calendar = Calendar.getInstance();

    DatePickerDialog dpd = DatePickerDialog.newInstance(
            this,
            calendar.get(Calendar.YEAR),
            calendar.get(Calendar.MONTH),
            calendar.get(Calendar.DAY_OF_MONTH)
    );
    dpd.show(getActivity().getFragmentManager(), "DatePickerDialog");

    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
    String a = "07-03-2018"; // example

    java.util.Date date = null;

    try {
        date = sdf.parse(a);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    calendar = dateToCalendar(date);
    System.out.println(calendar.getTime());

    List<Calendar> dates = new ArrayList<>();
    dates.add(calendar);
    Calendar[] disabledDays1 = dates.toArray(new Calendar[dates.size()]);
    dpd.setDisabledDays(disabledDays1);

}

    private Calendar dateToCalendar(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }




@Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {

    String date = dayOfMonth+"/"+(monthOfYear+1)+"/"+year;
    textview.setText(date);

}

call showDatePicker().

for multiple days

Change this

     String[] holidays = {"07-03-2018","05-03-2018","10-03-2018"};

    java.util.Date date = null;

    for (int i = 0;i < holidays.length; i++) {

        try {
            date = sdf.parse(holidays[i]);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        calendar = dateToCalendar(date);
        System.out.println(calendar.getTime());

        List<Calendar> dates = new ArrayList<>();
        dates.add(calendar);
        Calendar[] disabledDays1 = dates.toArray(new Calendar[dates.size()]);
        dpd.setDisabledDays(disabledDays1);
    }
like image 37
akash bs Avatar answered Sep 29 '22 15:09

akash bs


That is not possible with the android datepicker and you need to create a custom picker for yourself. See MaterialDateTimePicker

To disable Sundays you have to pass the array like this

      GregorianCalendar g1=new GregorianCalendar();
      g1.add(Calendar.DATE, 1);
      GregorianCalendar gc = new GregorianCalendar();
      gc.add(Calendar.DAY_OF_MONTH, 30);
       List<Calendar> dayslist= new LinkedList<Calendar>();
Calendar[] daysArray;
Calendar cAux = Calendar.getInstance();
while ( cAux.getTimeInMillis() <= gc.getTimeInMillis()) {
    if (cAux.get(Calendar.DAY_OF_WEEK) != 1) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(cAux.getTimeInMillis());
        dayslist.add(c);
    }
    cAux.setTimeInMillis(cAux.getTimeInMillis() + (24*60*60*1000));
}
daysArray = new Calendar[dayslist.size()];
for (int i = 0; i<daysArray.length;i++)
{
    daysArray[i]=dayslist.get(i);
}
datePickerDialog.setSelectableDays(daysArray);
like image 4
aenugula karthik Avatar answered Sep 29 '22 17:09

aenugula karthik