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