Krish
Krish

Reputation: 4232

DatePicker Dialogue Current Date Not getting to select

Hi I am new Android developer. In my App, I have created DatePickerDialogue.

My problem is when I select any dates other than current date then current date is not able to select.

My Code:

public class DatePickerDialogue extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.shared_layout);

    int mYear, mMonth, mDay;
    final Calendar c = Calendar.getInstance();
    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);

    DatePickerDialog dpd = new DatePickerDialog(DatePickerDialogue.this,
            new DatePickerDialog.OnDateSetListener() {

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

                }

            }, mYear, mMonth, mDay);

    dpd.getDatePicker().setMaxDate(System.currentTimeMillis());
    dpd.show();
}

enter image description here

enter image description here

Upvotes: 2

Views: 2401

Answers (2)

Harshad Pansuriya
Harshad Pansuriya

Reputation: 20930

Use this way

In below code I have OnclickListener on TextView and set Date to the TextView.

   int mYear, mMonth, mDay;

      final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);

        final TextView textView = (TextView)findViewById(R.id.textview23);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                DatePickerDialog dpd = new DatePickerDialog(MainActivity.this,
                        new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year, int month, int day) {
                                c.set(year, month, day);
                                String date = new SimpleDateFormat("MM/dd/yyyy").format(c.getTime());
                                textView.setText(date);

                                mYear = c.get(Calendar.YEAR);
                                mMonth = c.get(Calendar.MONTH);
                                mDay = c.get(Calendar.DAY_OF_MONTH);
                            }
                        }, mYear, mMonth, mDay);
                dpd.getDatePicker().setMaxDate(System.currentTimeMillis());

                Calendar d = Calendar.getInstance();

                dpd.updateDate(d.get(Calendar.YEAR),d.get(Calendar.MONTH),d.get(Calendar.DAY_OF_MONTH));
                dpd.show();


            }

        });

For Disable Future Date :

You should be able to call getDatePicker().setMaxDate(long) on your DatePickerDialog to set today as your max date. It will disable future Date.

Update :

for reset the Date whenever you want to default Date you have to update it is default value. like below code you can set it.

dpd.updateDate(d.get(Calendar.YEAR),d.get(Calendar.MONTH),d.get(Calendar.DAY_OF_MONTH));

Hope this will help you.

Upvotes: 2

akhil Rao
akhil Rao

Reputation: 1169

Try like this

   Calendar cal=Calendar.getInstance();
   int year=cal.get(Calendar.YEAR);
    int month=cal.get(Calendar.MONTH);
    int day=cal.get(Calendar.DAY_OF_WEEK);

    DatePickerDialog dp=new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {

        }
    },year,month,day);
dp.getDatePicker().setMaxDate(cal.getTimeInMillis());
 dp.show();

Upvotes: 0

Related Questions