Build “Date Picker” in Android studio. It should allow you to select day, month
ID: 3603833 • Letter: B
Question
Build “Date Picker” in Android studio. It should allow you to select day, month and year through its interface after the click of a button “PICK DATE” and then display it in a text view. The following screenshots show the final output.
Instructions:
You can build the date picker dialog as shown in chapter 4, or you can directly use the DatePickerDialog class provided in Android. To use this, you need to read extra resources over the Web.
Name your app with Alamri
Name the activities/java files as follows: Alamri_IT488
Screenshots of the app
Copy the whole xml code of your layout file(s).
Copy of the whole java code of the relevant classes
Explanation / Answer
The NewInstance method is invoked to instantiate a new DatePickerFragment. This method takes an Action<DateTime> that will be invoked when the user clicks on the OK button in the DatePickerDialog.
When the fragment is to be displayed, Android will call the method OnCreateDialog. This method will create a new DatePickerDialog object and initialize it with the current date and the callback object (which is the current instance of the DatePickerFragment).
Note: Be aware that the value of the month when IOnDateSetListener.OnDateSet is invoked is in the range of 0 to 11, and not 1 to 12. The day of the month will be in the range of 1 to 31 (depending on which month was selected).
Showing the DatePickerFragment
Now that the DialogFragment has been implemented, this section will examine how to use the fragment in an Activity. In the sample app that accompanies this guide, the Activity will instantiate the DialogFragment using the NewInstance factory method and then display it invoke DialogFragment.Show. As a part of instantiating the DialogFragment, the Activity passes an Action<DateTime>, which will display the date in a TextView that is hosted by the Activity:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.