Block days DatePickerFragment Android Studio

0

I need to block the days of DatePickerFragment , the days that can be released to select, will be counted from the current date + 3 days and the following days should be locked to select, or give a message if it day the user select was greater than that period. I tried in many ways but I was not successful, I follow below where I load the DatePickerFragment . Note: I'm using Android Studio

Button to call function

btnDataEntrega = (Button) findViewById(R.id.btnDataEntrega);
    btnDataEntrega.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            exibirDatePickerDialog();
        }
    });

Function to display DatePicker

 public void exibirDatePickerDialog() {
 DatePickerFragment fragment = new DatePickerFragment();

    fragment = DatePickerFragment.newInstance(this, new DatePickerFragment.DatePickerFragmentListener() {

        public void updateChangedDate(int dia, int mes, int ano) {
            btnDataEntrega.setText(String.valueOf(String.format("%02d", dia)) + "/" + String.valueOf(String.format("%02d", mes + 1)) + "/" + String.valueOf(ano));
            dataAtual.set(ano, mes, dia);
        }
    }, dataAtual);

    fragment.show(getFragmentManager(), "DatePickerFragment");
}

Function that loads DatePicker

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

static int dia;
static int mes;
static int ano;
static Context mContext;
static DatePickerFragmentListener mListener;

@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
    dia = dayOfMonth;
    mes = month;
    ano = year;

    mListener.updateChangedDate(dia, mes, ano);

}

public int show(FragmentTransaction transaction, String datePickerFragment) {

    return super.show(transaction, datePickerFragment);
}

public interface DatePickerFragmentListener {
    public void updateChangedDate(int dia, int mes, int ano);
}

public static DatePickerFragment newInstance(Context context, DatePickerFragmentListener listener, Calendar dataAtual) {
    DatePickerFragment dialog = new DatePickerFragment();
    mContext = context;
    mListener = listener;

    diaAtual = dataAtual.get(Calendar.DAY_OF_MONTH);
    dia = dataAtual.get(Calendar.DAY_OF_MONTH);
    mes = dataAtual.get(Calendar.MONTH);
    ano = dataAtual.get(Calendar.YEAR);

    Bundle args = new Bundle();
    args.putString("title", "Set Date");
    dialog.setArguments(args);


    return dialog;

}

public Dialog onCreateDialog(Bundle savedInstanceState) {
    return new DatePickerDialog(mContext, R.style.AppTheme_DialogTheme, mDataSetListener, ano, mes, dia);
}

private DatePickerDialog.OnDateSetListener mDataSetListener = new DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        dia = dayOfMonth;
        mes = monthOfYear;
        ano = year;

        mListener.updateChangedDate(dia, mes, ano);

    }
};
    
asked by anonymous 01.03.2018 / 14:53

1 answer

0

One way to do this is to use the add method to append the days to the current date and the setMaxDate method to range definition. See:

Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, 3);
dialog.setMaxDate(c);
    
01.03.2018 / 18:30