Install a DatePicker.OnDateChanged Listener into a DatePicker

Select a date using the yellow DatePicker. See the date you selected and the date that is 280 days later. Left, Samsung Galaxy S5. Right, Nexus 5.

Source code in DatePicker.zip

  1. MainActivity.java
  2. activity_main.xml. Each TextView and CalendarView is enclosed in a vertical LinearLayout. The two vertical LinearLayouts are enclosed in a horizontal LinearLayout.
  3. strings.xml: unchanged.
  4. res/values/dimens/dimens.xml. I added the dimension resource margin.
  5. res/values-w820dp/dimens.xml. I added the dimension resource margin.
  6. AndroidManifest.xml
  7. build.gradle (Module: app)

Documentation

  1. Pickers and Date pickers in the Material Design spec.
  2. Pickers and Date pickers in the User Interface guide.
  3. The Java class android.widget.DatePicker
    1. Documentation
    2. platform_frameworks_base/core/java/android/widget/DatePicker.java source code on GitHub
  4. The Java class android.widget.CalendarView
    1. Documentation
    2. platform_frameworks_base/core/java/android/widget/CalendarView.java source code on GitHub
  5. The Java class java.util.Calendar
    1. Documentation

Things to try

  1. The DatePicker occupies a lot of space on the screen. To make it disappear after you have entered the date, use a DatePickerDialog instead of a DatePicker. See Pickers.