I'm developing an app that requires user to select date(dd/mm/yyyy). I want to show a dialog box with DatePicker on button click. once date is selected it must show in EditText.
I'm using Android Studio 2.2, project with min sdk is 23.
Kindly help me to do required code.
I. In your build.gradle add latest appcompat library, at the time 24.2.1
dependencies {
compile 'com.android.support:appcompat-v7:X.X.X'
// where X.X.X version
}
II. Make your activity extend android.support.v7.app.AppCompatActivity
and implement the DatePickerDialog.OnDateSetListener
interface.
public class MainActivity extends AppCompatActivity
implements DatePickerDialog.OnDateSetListener {
III. Create your DatePickerDialog
setting a context, the implementation of the listener and the start year, month and day of the date picker.
DatePickerDialog datePickerDialog = new DatePickerDialog(
context, MainActivity.this, startYear, starthMonth, startDay);
IV. Show your dialog on the click event listener of your button
((Button) findViewById(R.id.myButton))
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
datePickerDialog.show();
}
});