In this article, will see how to implement DatePickerDialog into our Android Studio Project. DatePickerDialog helps you to get information from user in particular to date that is required in your application.
To implement DatePickerDialog, we need 5 arguments to be passed while creating the object.
context: You need to specify the application context i.e the current activity.
onDateSetListner: this method is invoked when the user has selected the date. onDateSet() method is use to take the date value that the user has set into DatePickerDialog.
day: It shows the current day, when the dialog is opened.
month: It shows the current month, when the dialog is opened.
year: It shows the current year, when the dialog is opened.
In onDateSet(), that is the overriding method of the OnDateSetListner helps you get the day, month and year value that the user has selected.
Steps and source code to implement DatePickerDialog:
– First Create a new Project DatePickerDialogExample.
– Create an empty activity i.e MainActivity.
– Now add TextView to your Activity layout file
activity_main.xml file:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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="com.test.datepickerexample.MainActivity"> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/textView" android:layout_centerHorizontal="true" android:layout_marginBottom="42dp" android:text="DatePicker Example" android:textSize="24sp" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="00/00/0000" android:textSize="24sp" /> </RelativeLayout>
– Then we will add the DatePickerDialog code into the MainActivity.java.
– In the TextView we are going to display the current date with the help of the Calendar class.
– After that we will be using onClickListener onto our TextView to open the DatePickerDialog.
– And then user selects the date and click OK. Thereafter, the date shown in TextView will be the date that user has selected.
MainActivity.java file:
package com.sanktips.datepickerexample; import android.app.DatePickerDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.DatePicker; import android.widget.TextView; import java.util.Calendar; public class MainActivity extends AppCompatActivity { Calendar currentDate; int mDay, mMonth, mShowMonth, mYear; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.textView); currentDate = Calendar.getInstance(); mDay = currentDate.get(Calendar.DAY_OF_MONTH); mMonth = currentDate.get(Calendar.MONTH); mYear = currentDate.get(Calendar.YEAR); mShowMonth = mMonth + 1; tv.setText(mDay + "/" + mShowMonth + "/" + mYear); tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker datePicker, int i, int i1, int i2) { i1 = i1 + 1; tv.setText(i2 + "/" + i1 + "/" + i); } }, mYear, mMonth, mDay); datePickerDialog.show(); } }); } }
– So, we are done with the implementation of DatePickerDialog. Run the project to see the result.
Hope you find this article helpful. Join us on Facebook, Twitter and Google+ to get more updates on Android Development Tutorials.