This Android datepicker tutorial will show you how to display the DatePickerDialog on button click, select a date and show the selected date in a TextView.
You can download the source code for the Android datepicker from here or follow the tutorial below.
File: MainActivity.java
package com.ahotbrew.datepicker; import android.app.DatePickerDialog; import android.app.Dialog; import android.support.v4.app.DialogFragment; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.DatePicker; import android.widget.TextView; import java.util.Calendar; public class MainActivity extends ActionBarActivity { public static TextView SelectedDateView; public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the current date as the default date in the picker final Calendar c = Calendar.getInstance(); int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH); int day = c.get(Calendar.DAY_OF_MONTH); // Create a new instance of DatePickerDialog and return it return new DatePickerDialog(getActivity(), this, year, month, day); } public void onDateSet(DatePicker view, int year, int month, int day) { SelectedDateView.setText("Selected Date: " + (month + 1) + "-" + day + "-" + year); } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); SelectedDateView = (TextView) findViewById(R.id.selected_date); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } public void showDatePickerDialog(View v) { DialogFragment newFragment = new DatePickerFragment(); newFragment.show(getSupportFragmentManager(), "datePicker"); } }
In line number 36 we display the selected date. You may notice that we are adding +1 to the month. This is because the month numbers go from 0 – 11 instead of 1 – 12. You can find here more to this.
File: activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/date_pick" android:onClick="showDatePickerDialog" android:id="@+id/button" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:id="@+id/selected_date" android:layout_below="@+id/button" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginTop="54dp" /> </RelativeLayout>
File: strings.xml
<resources> <string name="app_name">Ahotbrew.com Datepicker</string> <string name="date_pick">Pick a date</string> <string name="action_settings">Settings</string> </resources>
Sit back, take a sip from your hot brew and run your project.
Don’t forget to join or newsletter and get free android tutorials to enjoy with your hot brew.