Android Adventures - Pickers
Written by Mike James   
Tuesday, 08 October 2013
Article Index
Android Adventures - Pickers
Date Picker
Number Picker

Pickers are important ways to get user input, but they have been through so many revisions that they lack simple documentation or guidelines how to use them. Working with Pickers - date, time or number - can be a confusing mess. Let's see if we can make it all seem logical. 

 

Android Adventures With Android Studio

Contents

  1. Getting Started With Android Studio
  2. The Activity And The UI
  3. Building The UI and a Calculator App
  4. Lifecycle and State
  5. Basic Controls And Events
  6. Spinners
  7. Pickers
  8. UI Graphics A Deep Dive
  9. ListView And Adapters
  10. Introducing Fragments
  11. Fragments and XML
  12. Fragment And Activity Working Together
  13. Managing Fragments
  14. Custom dialogs using DialogFragment
  15. Dialog Classes In DialogFragment
  16. A NumberPicker DialogFragment Project

Androidgears

 

 

A picker is a "dial" that you can use to select one of a predetermined set of values. In this sense it is a lot like the Spinner covered in the previous installment, but one that has a more restricted set of choices. Android currently supports three Pickers for dates, times and general numbers. 

There is currently a small problem with Pickers and Android Studio that makes the UI editor fail to render the DatePicker and generally the use of Pickers is slightly fragile. 

Using A Picker

The first big confusion to clear up is that there are two ways to make use of a Picker - as a widget or as a dialog box. Of the three well-known Pickers, the TimePicker and the DatePicker come as widgets and as dialog boxes. The NumberPicker is only supplied as a widget, but of course you can put it in a Dialog box if you want to. 

In most cases you probably do want to create a Dialog box and in this case you need to use a DialogFragment to wrap the Dialog and manage its lifecycle. The only problem with this is that a Fragment is a complicated UI component and there are many situations where using the raw widget will do the job well enough. 

In short it makes sense to first look at the underlying Picker widgets before we get round to looking at Dialogs and DialogFragments in a later chapter.

Using TimePicker

TimePicker is a very easy place to get started. If you create a new Android project, called  TimeAndDate and accept all of the defaults you can place a TimePicker on the design surface just like any other widget - no need to worry about Fragments or Dialogs.  You can find the TimePicker in the Date & Time section of the Toolbox where you will also find the DatePicker.

 

Timetool

 

Place the TimePicker on the design surface and size and locate it as required. In its default configuration it shows in the designer using the style Theme.Light and it has buttons for the user to set the time:

 

TimePicker

 

 

However if you run the program, assuming you are targeting the latest version of Android you will see the newer Holo styled TimePicker:

 

Timeholo

 

The Holo style is arguably easier to use because the user can use touch to "spin" the selection and get to a value much quicker than in the old version. 

If you actually want to present the old style TimePicker, or any of the other Pickers, you have to force the Style to be Theme.Light or any of the other Theme styles. In principle you should be able to do this using Android Studio, but any changes in style that you select within the designer only changes what you see in the designer and not in the final running version. At the moment you have to make the style change manually by editing the XML resource files that control it. 

Style, and themes in general, is a big topic which will be covered in depth in another chapter. Do be aware for the moment, that much of the documentation refers to earler versions of Android. The current version uses the values/styles.xml file to determine the style used for API levels earlier than v11. For v11 and later there are specific XML style files stored in directories called values-vxy where xy is the version number. 

So if you are targetting SDK v14 you need to load and edit values-v14/styles.xml which contains by default:

<style name="AppBaseTheme"
 parent="android:Theme.Holo.Light.DarkActionBar">
<!-- API 14 theme customizations can go here. -->
</style>

What this means is that no matter what you change in values/styles the running program will use Holo.Light.DarkActionBar. 

If however you edit the file to read:

<style name="AppBaseTheme"
              parent="android:Theme.Light">
<!-- API 14 theme customizations can go here. -->
</style>

then the "classic" style will be used and you will see:

 

timeclassic

 

 

To interact with the TimePicker all you have to use is the get/set methods for CurrentHour and CurrentMinute. You can also programatically change the 12 hour/24 hour mode. Notice that the time is always returned in 24-hour form no matter what the widget's mode is. 

For example to set the TimePicker you would use

TimePicker tp=
      (TimePicker) findViewById(R.id.timePicker);
tp.setIs24HourView(true);
tp.setCurrentMinute(10);
tp.setCurrentHour(13);

The only task remaining is figuring out how to discover then the user has selected a time. You could provide a button which the user has to click to confirm the new time. 

For example, if you place a Button and a TextView on the design surface, add the following Button click event handler:

public void doButtonClick(View e){
 TextView tv= (TextView)
                 findViewById(R.id.textView);
 TimePicker tp= (TimePicker)
                 findViewById(R.id.timePicker);
 tv.setText(tp.getCurrentHour().toString() +
           ":"+tp.getCurrentMinute().toString());
}

and remember to set the Button's Click property to doButtonClick then you have a way for the user set the time. 

 

settime

 

In most cases the difficult part in using a Picker isn't setting it up or getting the data from it but in processing that data into a form that your program can use it in. In this case we simply convert the time into a slightly formatted string representation. 



Last Updated ( Friday, 17 October 2014 )
 
 

   
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.