How to popup date picker when clicking on edittext in Android Studio?

Please Share On:

So, you want to display a date picker calendar and allow the user to choose their favorite date from the date picker calendar. Then, follow my process. I am going to show you a simple and easy step on how you can add your date picker calendar upon click on date edittext. Here, is a sample example of a date picker calendar.

date picker calendar

Step 1: XML File

Create your edittext to display your date and apply the following code. Don’t forget to add the following code in your edittext.

android:focusable="false"

This makes your EditText allow for a single touch. Now your XML code looks like this

<EditText
    android:id="@+id/editTextDate"
    android:layout_width="182dp"
    android:layout_height="wrap_content"
    android:autofillHints="@string/hint_pick_up_date"
    android:focusable="false"
    android:background="#FFFFFF"
    android:ems="1"
    android:layout_weight="1"
    android:hint="@string/hint_pick_up_date"
    android:inputType="date"
    android:textColor="@color/black"
    android:textSize="15sp" />

Step 2: strings.xml

<resources>
<string name="hint_pick_up_date">dd/mm/yyyy</string>
</resources>

Step 3: Java File

EditText etdate = findViewById(R.id.editTextDate);

final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                myCalendar.set(Calendar.YEAR, year);
                myCalendar.set(Calendar.MONTH, month);
                myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                updateDate();
            }
        };

        etDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new DatePickerDialog(Home.this, date, myCalendar
                        .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                        myCalendar.get(Calendar.DAY_OF_MONTH)).show();
            }
        });

Step 4: Add Method

private void updateDate() {
        String myFormat = "dd/MM/yy"; //put your date format in which you need to display
        SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.ENGLISH);

        etDate.setText(sdf.format(myCalendar.getTime()));
    }


You may interest on the following topics:

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright @2023. All Right Reserved.


Social media & sharing icons powered by UltimatelySocial