Bahu
Bahu

Reputation: 1596

Android: Getting previous date in DatePickerDialog

i'm successfully displaying the DatePickerDialog with restricting the previous date by using datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());. In this case it not activating the previous dates and restricting the previous months but user still able to select the previous date of present month (For understanding see the image and code)

enter image description here

etDate.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

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


            DatePickerDialog datePickerDialog = new DatePickerDialog(EventMyEventEdit.this, new OnDateSetListener() {

                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear,
                        int dayOfMonth) {
                     mcurrentDate.set(Calendar.YEAR, year);
                     mcurrentDate.set(Calendar.MONTH, monthOfYear);
                     mcurrentDate.set(Calendar.DAY_OF_MONTH,dayOfMonth);
                     String myFormat = "dd/MM/yy";
                     SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

                     etDate.setText(sdf.format(mcurrentDate.getTime()));

                }
            }, mYear, mMonth, mDay);

            datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());

            datePickerDialog.show();

        }
    });

Edit: I want to restrict the user when selecting the previous dates than the present date

Upvotes: 0

Views: 1438

Answers (2)

Bahu
Bahu

Reputation: 1596

Thanks to Dhiraj. I solved the issue by changing the code as Dhiraj suggestion

imports

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import android.support.v7.app.AppCompatActivity;
import android.widget.DatePicker;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;

Code

etDate.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

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

                DatePickerDialog datePickerDialog = new DatePickerDialog(
                        EventMyEventEdit.this, AlertDialog.THEME_DEVICE_DEFAULT_DARK, new OnDateSetListener() {

                            @Override
                            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                                mcurrentDate.set(Calendar.YEAR, year);
                                mcurrentDate.set(Calendar.MONTH, monthOfYear);
                                mcurrentDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                                String myFormat = "dd/MM/yy";
                                SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

                                etDate.setText(sdf.format(mcurrentDate.getTime()));

                            }
                        }, mYear, mMonth, mDay);


                datePickerDialog.getDatePicker().setMinDate(new Date().getTime() - 10000);

                datePickerDialog.show();

            }
        });

Upvotes: 1

Dhiraj
Dhiraj

Reputation: 910

I had same problem and resolve it by changing theme of DatePickerDialog.I dont know why it depends on theme in my case but it worked for me

use following code

 DatePickerDialog _date = new DatePickerDialog(this, AlertDialog.THEME_HOLO_LIGHT, datePickerListener, myear, mmonth,
                        mday) {
                    @Override
                    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

                        view.setSpinnersShown(true);
                        Calendar c = Calendar.getInstance();
                        int myear = c.get(Calendar.YEAR);
                        int mmonth = c.get(Calendar.MONTH);
                        int mday = c.get(Calendar.DAY_OF_MONTH);

                        if (year < myear)
                            view.updateDate(myear, mmonth, mday);

                        if (monthOfYear < mmonth && year == myear)
                            view.updateDate(myear, mmonth, mday);

                        if (dayOfMonth < mday && year == myear && monthOfYear == mmonth)
                            view.updateDate(myear, mmonth, mday);

                    }
                };

Upvotes: 1

Related Questions