164461bf4f261164cb9e3022761fd217fd0028ac5Scott Mainpage.title=Pickers 233baa5ad7d8cdcc89ce4fbc3bc8cd537d5f5d639Joe Fernandezpage.tags=datepicker,timepicker 350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main@jd:body 450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<div id="qv-wrapper"> 650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<div id="qv"> 750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<h2>In this document</h2> 850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<ol> 950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main <li><a href="#TimePicker">Creating a Time Picker</a> 1050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main <ol> 1150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main <li><a href="#TimePickerFragment">Extending DialogFragment for a time picker</a></li> 1250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main <li><a href="#ShowingTheTimePicker">Showing the time picker</a></li> 1350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main </ol> 1450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main </li> 1550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main <li><a href="#DatePicker">Creating a Date Picker</a> 1650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main <ol> 1750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main <li><a href="#DatePickerFragment">Extending DialogFragment for a date picker</a></li> 1850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main <li><a href="#ShowingTheDatePicker">Showing the date picker</a></li> 1950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main </ol> 2050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main </li> 2150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</ol> 2250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main <h2>Key classes</h2> 2350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main <ol> 2450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main <li>{@link android.app.DatePickerDialog}</li> 2550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main <li>{@link android.app.TimePickerDialog}</li> 2650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main <li>{@link android.support.v4.app.DialogFragment}</li> 2750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main </ol> 2850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main <h2>See also</h2> 2950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main <ol> 3050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main <li><a href="{@docRoot}guide/components/fragments.html">Fragments</a></li> 3150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main </ol> 3250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</div> 3350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</div> 3450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 3550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>Android provides controls for the user to pick a time or pick a date as ready-to-use dialogs. 3650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott MainEach picker provides controls for selecting each part of the time (hour, minute, AM/PM) or date 3750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main(month, day, year). Using these pickers helps ensure that your users can pick a time or date that 3850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainis valid, formatted correctly, and adjusted to the user's locale.</p> 3950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 4050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<img src="{@docRoot}images/ui/pickers.png" alt="" /> 4150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 4250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>We recommend that you use {@link android.support.v4.app.DialogFragment} to host each time or date 4350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainpicker. The {@link android.support.v4.app.DialogFragment} manages the dialog lifecycle for you and 4450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainallows you to display the pickers in different layout configurations, 4550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainsuch as in a basic dialog on handsets or as an embedded part of the layout on large screens.</p> 4650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 4750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>Although {@link android.app.DialogFragment} was first added to the platform in Android 3.0 (API 4850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainlevel 11), if your app supports versions of Android older than 3.0—even as low as Android 4950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main1.6—you can use the {@link android.support.v4.app.DialogFragment} class that's available in 504e2c9dc74b74b1ba10793177475a599f75fbb18bScott Mainthe <a href="{@docRoot}tools/support-library/index.html">support library</a> for backward 5150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Maincompatibility.</p> 5250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 5350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p class="note"><strong>Note:</strong> The code samples below show how to create dialogs for a time 544e2c9dc74b74b1ba10793177475a599f75fbb18bScott Mainpicker and date picker using the <a href="{@docRoot}tools/support-library/index.html">support 5550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainlibrary</a> APIs for {@link android.support.v4.app.DialogFragment}. If your app's <a 5650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainhref="{@docRoot}guide/topics/manifest/uses-sdk-element.html#min">{@code minSdkVersion}</a> is 11 or 5750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainhigher, you can instead use the platform version of {@link android.app.DialogFragment}.</p> 5850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 5950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 6050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 6150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<h2 id="TimePicker">Creating a Time Picker</h2> 6250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 6350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>To display a {@link android.app.TimePickerDialog} using {@link 6450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.support.v4.app.DialogFragment}, you need to define a fragment class that extends {@link 6550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.support.v4.app.DialogFragment} and return a {@link android.app.TimePickerDialog} from the 6650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainfragment's {@link android.support.v4.app.DialogFragment#onCreateDialog onCreateDialog()} method.</p> 6750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 6850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p class="note"><strong>Note:</strong> If your app supports versions of Android older than 3.0, 6950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainbe sure you've set up your Android project with the support library as described in <a 704e2c9dc74b74b1ba10793177475a599f75fbb18bScott Mainhref="{@docRoot}tools/support-library/setup.html">Setting Up a Project to Use a 7150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott MainLibrary</a>.</p> 7250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 7350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<h3 id="TimePickerFragment">Extending DialogFragment for a time picker</h3> 7450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 7550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>To define a {@link 7650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.support.v4.app.DialogFragment} for a {@link android.app.TimePickerDialog}, you 7750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainmust:</p> 7850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<ul> 7950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main <li>Define the {@link android.support.v4.app.DialogFragment#onCreateDialog onCreateDialog()} 8050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainmethod to return an instance of {@link android.app.TimePickerDialog}</li> 8150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main <li>Implement the 8250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main{@link android.app.TimePickerDialog.OnTimeSetListener} interface to receive a callback when the user 8350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainsets the time.</li> 8450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</ul> 8550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 8650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>Here's an example:</p> 8750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 8850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<pre> 8950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainpublic static class TimePickerFragment extends DialogFragment 9050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main implements TimePickerDialog.OnTimeSetListener { 9150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 9250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main @Override 9350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main public Dialog onCreateDialog(Bundle savedInstanceState) { 9450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main // Use the current time as the default values for the picker 9550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main final Calendar c = Calendar.getInstance(); 9650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main int hour = c.get(Calendar.HOUR_OF_DAY); 9750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main int minute = c.get(Calendar.MINUTE); 9850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 9950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main // Create a new instance of TimePickerDialog and return it 10050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main return new TimePickerDialog(getActivity(), this, hour, minute, 10150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main DateFormat.is24HourFormat(getActivity())); 10250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main } 10350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 10450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 10550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main // Do something with the time chosen by the user 10650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main } 10750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main} 10850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</pre> 10950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 11050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>See the {@link android.app.TimePickerDialog} class for information about the constructor 11150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainarguments.</p> 11250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 11350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>Now all you need is an event that adds an instance of this fragment to your activity.</p> 11450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 11550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 11650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<h3 id="ShowingTheTimePicker">Showing the time picker</h3> 11750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 11850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>Once you've defined a {@link android.support.v4.app.DialogFragment} like the one shown above, 11950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainyou can display the time picker by creating an instance of the {@link 12050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.support.v4.app.DialogFragment} and calling {@link 12150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.support.v4.app.DialogFragment#show show()}.</p> 12250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 12350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>For example, here's a button that, when clicked, calls a method to show the dialog:</p> 12450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 12550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<pre> 12650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<Button 12750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main android:layout_width="wrap_content" 12850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main android:layout_height="wrap_content" 12950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main android:text="@string/pick_time" 13050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main android:onClick="showTimePickerDialog" /> 13150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</pre> 13250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 13350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>When the user clicks this button, the system calls the following method:</p> 13450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 13550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<pre> 13650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainpublic void showTimePickerDialog(View v) { 13750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main DialogFragment newFragment = new TimePickerFragment(); 13850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main newFragment.show(getSupportFragmentManager(), "timePicker"); 13950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main} 14050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</pre> 14150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 14250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>This method calls {@link 14350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.support.v4.app.DialogFragment#show show()} on a new instance of the {@link 14450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.support.v4.app.DialogFragment} defined above. The {@link 14550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.support.v4.app.DialogFragment#show show()} method requires an instance of {@link 14650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.support.v4.app.FragmentManager} and a unique tag name for the fragment.</p> 14750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 14850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p class="caution"><strong>Caution:</strong> If your app supports versions of Android lower than 14950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main3.0, be sure that you call {@link 15050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.support.v4.app.FragmentActivity#getSupportFragmentManager()} to acquire an instance of 15150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main{@link android.support.v4.app.FragmentManager}. Also make sure that your activity that displays the 15250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Maintime picker extends {@link android.support.v4.app.FragmentActivity} instead of the standard {@link 15350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.app.Activity} class.</p> 15450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 15550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 15650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 15750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 15850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 15950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 16050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 16150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 16250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 16350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<h2 id="DatePicker">Creating a Date Picker</h2> 16450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 16550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>Creating a {@link android.app.DatePickerDialog} is just like creating a {@link 16650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.app.TimePickerDialog}. The only difference is the dialog you create for the fragment.</p> 16750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 16850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>To display a {@link android.app.DatePickerDialog} using {@link 16950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.support.v4.app.DialogFragment}, you need to define a fragment class that extends {@link 17050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.support.v4.app.DialogFragment} and return a {@link android.app.DatePickerDialog} from the 17150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainfragment's {@link android.support.v4.app.DialogFragment#onCreateDialog onCreateDialog()} method.</p> 17250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 17350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p class="note"><strong>Note:</strong> If your app supports versions of Android older than 3.0, 17450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainbe sure you've set up your Android project with the support library as described in <a 1754e2c9dc74b74b1ba10793177475a599f75fbb18bScott Mainhref="{@docRoot}tools/support-library/setup.html">Setting Up a Project to Use a 17650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott MainLibrary</a>.</p> 17750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 17850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<h3 id="DatePickerFragment">Extending DialogFragment for a date picker</h3> 17950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 18050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>To define a {@link 18150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.support.v4.app.DialogFragment} for a {@link android.app.DatePickerDialog}, you 18250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainmust:</p> 18350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<ul> 18450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main <li>Define the {@link android.support.v4.app.DialogFragment#onCreateDialog onCreateDialog()} 18550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainmethod to return an instance of {@link android.app.DatePickerDialog}</li> 18650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main <li>Implement the 18750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main{@link android.app.DatePickerDialog.OnDateSetListener} interface to receive a callback when the user 18850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainsets the date.</li> 18950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</ul> 19050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 19150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>Here's an example:</p> 19250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 19350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<pre> 19450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainpublic static class DatePickerFragment extends DialogFragment 19550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main implements DatePickerDialog.OnDateSetListener { 19650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 19750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main @Override 19850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main public Dialog onCreateDialog(Bundle savedInstanceState) { 19950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main // Use the current date as the default date in the picker 20050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main final Calendar c = Calendar.getInstance(); 20150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main int year = c.get(Calendar.YEAR); 20250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main int month = c.get(Calendar.MONTH); 20350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main int day = c.get(Calendar.DAY_OF_MONTH); 20450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 20550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main // Create a new instance of DatePickerDialog and return it 20650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main return new DatePickerDialog(getActivity(), this, year, month, day); 20750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main } 20850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 20950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main public void onDateSet(DatePicker view, int year, int month, int day) { 21050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main // Do something with the date chosen by the user 21150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main } 21250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main} 21350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</pre> 21450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 21550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>See the {@link android.app.DatePickerDialog} class for information about the constructor 21650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainarguments.</p> 21750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 21850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>Now all you need is an event that adds an instance of this fragment to your activity.</p> 21950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 22050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 22150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<h3 id="ShowingTheDatePicker">Showing the date picker</h3> 22250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 22350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>Once you've defined a {@link android.support.v4.app.DialogFragment} like the one shown above, 22450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainyou can display the date picker by creating an instance of the {@link 22550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.support.v4.app.DialogFragment} and calling {@link 22650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.support.v4.app.DialogFragment#show show()}.</p> 22750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 22850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>For example, here's a button that, when clicked, calls a method to show the dialog:</p> 22950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 23050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<pre> 23150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<Button 23250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main android:layout_width="wrap_content" 23350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main android:layout_height="wrap_content" 23450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main android:text="@string/pick_date" 23550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main android:onClick="showDatePickerDialog" /> 23650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</pre> 23750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 23850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>When the user clicks this button, the system calls the following method:</p> 23950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 24050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<pre> 24150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainpublic void showDatePickerDialog(View v) { 24250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main DialogFragment newFragment = new DatePickerFragment(); 24350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main newFragment.show(getSupportFragmentManager(), "datePicker"); 24450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main} 24550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</pre> 24650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 24750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>This method calls {@link 24850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.support.v4.app.DialogFragment#show show()} on a new instance of the {@link 24950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.support.v4.app.DialogFragment} defined above. The {@link 25050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.support.v4.app.DialogFragment#show show()} method requires an instance of {@link 25150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.support.v4.app.FragmentManager} and a unique tag name for the fragment.</p> 25250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main 25350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p class="caution"><strong>Caution:</strong> If your app supports versions of Android lower than 25450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main3.0, be sure that you call {@link 25550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.support.v4.app.FragmentActivity#getSupportFragmentManager()} to acquire an instance of 25650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main{@link android.support.v4.app.FragmentManager}. Also make sure that your activity that displays the 25750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Maintime picker extends {@link android.support.v4.app.FragmentActivity} instead of the standard {@link 25850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.app.Activity} class.</p> 259