Android Programming The Big Nerd Ranch Guide, 3rd Edition

(Brent) #1
Setting a dialog’s contents

233

In DatePickerFragment.onCreateDialog(Bundle), inflate this view and then set it on the dialog.


Listing 12.4  Adding DatePicker to AlertDialog
(DatePickerFragment.java)


@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View v = LayoutInflater.from(getActivity())
.inflate(R.layout.dialog_date, null);


return new AlertDialog.Builder(getActivity())
.setView(v)
.setTitle(R.string.date_picker_title)
.setPositiveButton(android.R.string.ok, null)
.create();
}


Run CriminalIntent. Press the date button to confirm that the dialog now presents a DatePicker. As
long as you are running Lollipop or later, you will see a calendar picker (Figure 12.6).


Figure 12.6  DatePicker with a calendar


The calendar picker in Figure 12.6 was introduced along with material design. This version of the
DatePicker widget ignores the calendarViewShown attribute you set in your layout. If you are running
a previous version of Android, however, you will see the old spinner-based DatePicker version that
respects that attribute (Figure 12.7).


http://www.ebook3000.com

Free download pdf