To provide a widget for selecting a date, use the DatePicker widget, which allows the user to select the month, day, and year, in a familiar interface.

In this article, I am going to explain how to create a DatePickerDialog, which presents the date picker in a floating dialog box at the press of a button. When the date is set by the user, a TextView will update with the new date.

·         Start a new project named DatePickerDemo.

·         Open the res/layout/main.xml file and insert the following:

<?xml  version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
       android:id="@+id/dateDisplay"
       android:layout_marginTop="10dp" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<Button android:id="@+id/pickDate"
              android:layout_marginTop="10dp"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Change the date"    />
</LinearLayout>
  • Open Activity file and add the following code as shown below:
import java.util.Calendar;
 
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
 
public class DatePickerActivity extends Activity {
   
       private TextView mDateDisplay;
       private Button mPickDate;
       private int mYear;
       private int mMonth;
       private int mDay;
       static final int DATE_DIALOG_ID = 0;
      
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        // capture or view elements
        mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
        mPickDate = (Button) findViewById(R.id.pickDate);
       
        // add a click listener on the button
        mPickDate.setOnClickListener(new OnClickListener() {
                    
                     public void onClick(View v) {
                           showDialog(DATE_DIALOG_ID);
                     }
              });
       
        // get the current date
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);
       
        updateDisplay();
    }
   
    // updates the date in the TextView   
    private void updateDisplay()
    {       
       mDateDisplay.setText(new StringBuilder().append(mMonth + 1).append("-
                            "
                            ").append(mDay).append("-").append(mYear).append(" "));   
    }
   
    // the callback received when the user "sets" the date in the dialog
    private DatePickerDialog.OnDateSetListener mDateSetListener = new 
                                                DatePickerDialog.OnDateSetListener()
                                                DatePickerDialog.OnDateSetListener()
              {
 
                           public void onDateSet(DatePicker view, int year,
                                         int monthOfYear, int dayOfMonth) {
                                 
                                   mYear = year;                   
                                   mMonth = monthOfYear;                   
                                   mDay = dayOfMonth;                  
                                   updateDisplay();
                           }
              };
   
    @Override
    protected Dialog onCreateDialog(int id)
    {
       switch(id)
       {
       case DATE_DIALOG_ID:
              return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,mDay);
       }
       return null;
    }
}
  •  Run the application.

You output looks like something this:

Data Picker in Android Application

When you click on the button (Change the date), a date picker dialog will open, you can set the date from the date picker dialog.

Data Picker in Android Application

After setting the date, a new date will display in the TextView.

Leave Comment