4

I am new to Android programming. Please help.

I am using Fragment that creates Material design DatePickerDialog on click of EditText. Trouble is it is set to current date (set by me). But, if user has to select the date in the past ... say, 10 years ago, user has to scroll each month which is painful. e.g. shown below:

Is there a way to make your select the year? This way user can navigate to the year.

enter image description here

Raj Lalwani
  • 391
  • 1
  • 6
  • 14

3 Answers3

3

At least on Android 7, you can click on the year and get a year selection dialog: enter image description here

cweiske
  • 30,033
  • 14
  • 133
  • 194
  • 1
    yeah but how to get this first? many users can't figure out that they can click on the year to advance much quicker to the past – user1623521 Nov 24 '19 at 11:37
1

I think than that is because you don't have a minimum date or maxim date. If you try to create a custom dialog with DatePicker, it works. I have a same example with an Edittext. I call this method when the user to click in Edittext.

private void showDateDialog() {
    mLayoutInflater = getLayoutInflater();
    mCustomDatePicker = mLayoutInflater.inflate(R.layout.custom_date_picker, null);

    mDatePicker = (DatePicker) mCustomDatePicker.findViewById(R.id.datePicker);
    mDatePicker.setMaxDate(Calendar.getInstance().getTime().getTime());

    mCalendar = Calendar.getInstance();

    mDialog = new AlertDialog.Builder(this);
    mDialog.setView(mCustomDatePicker);
    mDialog.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            mCalendar.set(mDatePicker.getYear(), mDatePicker.getMonth(), mDatePicker.getDayOfMonth());
            mBirthdayEdit.setText(mFormatDate.format(mCalendar.getTime()));

        }
    }).setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });

    mDialog.create();
    mDialog.show();
}

You layout

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <DatePicker
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/datePicker"
        android:layout_gravity="center_horizontal"
        android:spinnersShown="true"
        android:calendarViewShown="false"
        android:layout_alignParentTop="true"/>
</RelativeLayout>
Juan Labrador
  • 1,204
  • 10
  • 14
0
    public void setMaxDate() {
        final Calendar currentDate = Calendar.getInstance();
        date = Calendar.getInstance();

        DatePickerDialog.OnDateSetListener dateSetListener = new

                DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker datePicker, int year, int
                            monthOfYear, int dayOfMonth) {
                        date.set(year, monthOfYear, dayOfMonth);

                        //use this date as per your requirement
                    }
                };
        DatePickerDialog datePickerDialog = new
                DatePickerDialog(EditProfileActivity.this, dateSetListener,
                currentDate.get(Calendar.YEAR),
                currentDate.get(Calendar.MONTH ),
                currentDate.get(Calendar.DAY_OF_MONTH));
        // Limiting access to past dates in the step below:


Calendar calendar = Calendar.getInstance();
                    calendar.add(Calendar.YEAR, -14);
                    datePickerDialog.getDatePicker().setMaxDate(calendar.getTimeInMillis());
                    datePickerDialog.show();
    }

This is working code ,you can set 14 year ago max date

Enjoy...!

user1623521
  • 340
  • 1
  • 16
Mahi Saini
  • 219
  • 2
  • 2