How to show DatePicker when you click on the edit box.
We will create a simple date dialog.
When a person clicks on the edited text, the popup window shows the selected date.
activity_main.xml
MainActivity.java
watch live demo
SUBSCRIBE to download
Download Source code
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="date" android:hint="DD/MM/YYYY" android:id="@+id/edtdate" android:focusable="false" android:ems="15"> </EditText> </LinearLayout>
package com.example.selectdate; import androidx.appcompat.app.AppCompatActivity; import android.app.DatePickerDialog; import android.os.Bundle; import android.view.View; import android.widget.DatePicker; import android.widget.EditText; import java.util.Calendar; public class MainActivity extends AppCompatActivity implements View.OnClickListener { EditText selectdate; private int mYear, mMonth, mDay; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate ( savedInstanceState ); setContentView ( R.layout.activity_main ); selectdate = findViewById ( R.id.edtdate ); selectdate.setOnClickListener ( this ); } @Override public void onClick(View v) { if (v == selectdate) { final Calendar calendar = Calendar.getInstance (); mYear = calendar.get ( Calendar.YEAR ); mMonth = calendar.get ( Calendar.MONTH ); mDay = calendar.get ( Calendar.DAY_OF_MONTH ); //show dialog DatePickerDialog datePickerDialog = new DatePickerDialog ( this, new DatePickerDialog.OnDateSetListener () { @Override public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { selectdate.setText ( dayOfMonth + "/" + (month + 1) + "/" + year ); } }, mYear, mMonth, mDay ); datePickerDialog.show (); } } }
https://youtu.be/rtreQEu7ye4
Download Source code