0

Show Date Dialog Picker

Dear All. I'm testing with Date Picker Dialog, when I setMax it'll show look like image. But, now I would like to show day left of the month. For example, I set max which is 20/07/2017, it only show the first day of next month(01/08/2017), it hide days from 21/07 - 30/07. Can everyone help me. Thank you so much!

And this is my code

public class MainActivity extends AppCompatActivity {

@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button button = (Button) findViewById(R.id.but);

    final Calendar calendar = Calendar.getInstance();
    final int year = calendar.get(Calendar.YEAR);
    final int month = calendar.get(Calendar.MONTH);
    final int day = calendar.get(Calendar.DAY_OF_MONTH);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            DatePickerDialog dialog = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {

                }
            }, year, month, day);
            //dialog.getDatePicker().setMinDate(System.currentTimeMillis());
            dialog.getDatePicker().setMaxDate(calendar.getTimeInMillis());
            dialog.show();
        }
    });
}

1 Answers1

0

I think you can try below code:

      @SuppressLint("ValidFragment")
public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
    public SelectDateFragment() {
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        DatePickerDialog datePickerDialog;
        int yy = calendar.get(Calendar.YEAR);
        int mm = calendar.get(Calendar.MONTH);
        int dd = calendar.get(Calendar.DAY_OF_MONTH);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
            datePickerDialog=new DatePickerDialog(this,android.R.style.Theme_Material_Light_Dialog, this, yy, mm, dd);
        else
            datePickerDialog=new DatePickerDialog(this, this, yy, mm, dd);

        Calendar maxDate=Calendar.getInstance();
        maxDate.add(Calendar.DATE, 1);

     // here you are adding maxdate 
   datePickerDialog.getDatePicker().setMaxDate(maxDate.getTimeInMillis());
        return datePickerDialog;
    }

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

       //  get the date here and use it

    }
}

and here's how you can use the above class

  • Declare an object first

     DialogFragment dateFragment = new SelectDateFragment();
    
  • Show it on click of some button or something

     dateFragment.show(getFragmentManager(), "DatePicker");
    
Uday Ramjiyani
  • 1,407
  • 9
  • 22