Android Adventures - Pickers
Written by Mike James   
Thursday, 10 September 2015
Article Index
Android Adventures - Pickers
Coding the TimePicker
Date Picker
Number Picker
Multi-Digit Input
Summary & Conclusion

Summary

  • The Pickers - TimePicker, DatePicker and NumberPicker provide easy to use ways of getting user input without having to use an on-screen keyboard. 

  • Pickers are usually introduced as dialog boxes or dialog fragments. This is a good way to use them, but they are also useful within a general layout.

  • With the introduction of Material Design with Android Lollipop, the look of the Pickers has changed. For easy backward compatibility use compatibility library v7 appcompat, which is included in your projects automatically by Android Studio, and stick with the AppCompat themes.

  • If you stick with these defaults your app will use Material Design on Lollipop and later, but the Holo theme for earlier versions of Android.
     
  • If you stray from AppCompat themes things tend not to work.

  • It is possible and very easy to replace the Material Design with the Holo look on all versions of Android. Simply set the  timePickerMode and/or datePickerMode properties to spinner. The NumberPicker always shows as a spinner. 

  • Use the OnXChangedListener event handler to respond to user input.
     
  • The DatePicker in spinner form can also display a calender using the CalendarViewShown property.

  • The designer isn't very stable. If it complains about missing classes or styles try ignoring the errors and running the project. This often clears the errors and allows the Pickers to render correctly. 

 

Conclusion 

There is much more to say about the Pickers and how to customize them, but the methods explained here are the most common and customization generally only involves the use of fairly obvious properties and methods. 

The biggest omission here is the use of the Pickers within a DialogFragment. This is a big topic that is covered in  Android Adventures - Mastering Fragments .

 

You can download the code for the programs from the CodeBin (note you have to register first).

 

androidJavaSmallAndroid Programming In Java:
Starting With an App
Third Edition

Is now available in paperback and ebook.

Available from Amazon.

 

 

  1. Getting Started With Android Studio 3
  2. The Activity And The UI
  3. Building The UI and a Calculator App
  4. Android Events
         Extract: Using Lambdas
  5. Basic Controls
  6. Layout Containers
  7. The ConstraintLayout
        Extract: Guidelines and Barriers
  8. UI Graphics A Deep Dive
        Extract: Programming the UI ***NEW
  9. Menus & The Action Bar
  10. Menus, Context & Popup
  11. Resources
  12. Beginning Bitmap Graphics
        Extract: Simple Animation
  13. Staying Alive! Lifecycle & State
  14. Spinners
  15. Pickers
  16. ListView And Adapters

If you are interested in creating custom template also see:

Custom Projects In Android Studio

Androidgears

 

 

To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter,FacebookGoogle+ or Linkedin,  or sign up for our weekly newsletter.

 

espbook

 

Comments




or email your comment to: comments@i-programmer.info

 


Last Updated ( Saturday, 15 October 2016 )