Block days Datepickerfragment Android Studio

Asked

Viewed 330 times

0

I need to block the days of Datepickerfragment, the days that may be released to select, will be counted from the current date + 3 days and the following days should be locked to select, or else give a message if it day that the user selected was greater than that period. I tried several ways but I did not succeed, follows below where I carry the Datepickerfragment.
Note: I am using Android Studio

Button to call the function

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

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

Function to display the 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 carries the 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);

    }
};

1 answer

1

One way to do this is to use the method add to add the days on the current date and the method setMaxDate for the definition of crease. Behold:

Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, 3);
dialog.setMaxDate(c);
  • Viana, it didn’t work, I’m posting the full code of the function and button where I call the Datepicker

Browser other questions tagged

You are not signed in. Login or sign up in order to post.