Exploring User Interface Screen Elements

Exploring User Interface Screen Elements

The Android SDK provides a couple controls for getting date and time input from the user.The first is the DatePicker control (Figure 7.8, top). It can be used to get a month, day, and year from the user. The basic XML layout resource definition for a DatePicker follows: As you can see from this example, there aren’t any attributes specific to the DatePicker control.As with many of the other controls, your code can register to receive a method call when the date changes.You do this by implementing the onDateChanged() method. However, this isn’t done the usual way. final DatePicker date = (DatePicker)findViewById(R.id.DatePicker01); date.init(date.getYear(), date.getMonth(), date.getDayOfMonth(), new DatePicker.OnDateChangedListener() { public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { Date dt = new Date(year-1900, Figure 7.8 Date and time controls. Getting Dates and Times from Users Using Indicators to Display Data to Users 151 monthOfYear, dayOfMonth, time.getCurrentHour(), time.getCurrentMinute()); text.setText(dt.toString()); } }); The preceding code sets the DatePicker.OnDateChangedListener by a call to the DatePicker.init() method.A DatePicker control is initialized with the current date.A TextView is set with the date value that the user entered into the DatePicker control. The value of 1900 is subtracted from the year parameter to make it compatible with the java.util.Date class. A TimePicker control (also shown in Figure 7.8, bottom) is similar to the DatePicker control. It also doesn’t have any unique attributes. However, to register for a method call when the values change, you call the more traditional method of TimePicker.setOnTimeChangedListener(). time.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() { public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { Date dt = new Date(date.getYear()-1900, date.getMonth(), date.getDayOfMonth(), hourOfDay, minute); text.setText(dt.toString()); } }); As in the previous example, this code also sets a TextView to a string displaying the time value that the user entered.When you use the DatePicker control and the TimePicker control together, the user can set a full date and time.

Using Indicators to Display Data to Users

The Android SDK provides a number of controls that can be used to visually show some form of information to the user.These indicator controls include progress bars, clocks, and other similar controls. Indicating Progress with ProgressBar Applications commonly perform actions that can take a while.A good practice during this time is to show the user some sort of progress indicator that informs the user that the application is off “doing something.”Applications can also show how far a user is through some operation, such as a playing a song or watching a video.The Android SDK provides several types of progress bars. The standard progress bar is a circular indicator that only animates. It does not show how complete an action is. It can, however, show that something is taking place.This is useful when an action is indeterminate in length.There are three sizes of this type of progress indicator (see Figure 7.9).  The second type is a horizontal progress bar that shows the completeness of an action. (For example, you can see how much of a file is downloading.) This horizontal progress bar can also have a secondary progress indicator on it.This can be used, for instance, to show the completion of a downloading media file while that file plays. This is an XML layout resource definition for a basic indeterminate progress bar: The default style is for a medium-size circular progress indicator; not a “bar” at all.The other two styles for indeterminate progress bar are progressBarStyleLarge and progressBarStyleSmall.This style animates automatically.The next sample shows the layout definition for a horizontal progress indicator:  We have also set the attribute for max in this sample to 100.This can help mimic a percentage progress bar.That is, setting the progress to 75 shows the indicator at 75 percent complete. We can set the indicator progress status programmatically as follows: mProgress = (ProgressBar) findViewById(R.id.progress_bar); mProgress.setProgress(75); You can also put these progress bars in your application’s title bar (as shown in Figure 7.9). This can save screen real estate.This can also make it easy to turn on and off an indeterminate progress indicator without changing the look of the screen. Indeterminate progress indicators are commonly used to display progress on pages where items need to be loaded before the page can finish drawing.This is often employed on web browser screens.The following code demonstrates how to place this type of indeterminate progress indicator on your Activity screen: requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); requestWindowFeature(Window.FEATURE_PROGRESS); setContentView(R.layout.indicators); setProgressBarIndeterminateVisibility(true); setProgressBarVisibility(true); setProgress(5000); To use the indeterminate indicator on your Activity objects title bar, you need to request the feature Window.FEATURE_INDETERMINATE_PROGRESS, as previously shown.This shows a small circular indicator in the right side of the title bar. For a horizontal progress bar style that shows behind the title, you need to enable the Window.FEATURE_PROGRESS. These features must be enabled before your application calls the setContentView() method, as shown in the preceding example. You need to know about a couple of important default behaviors. First, the indicators are visible by default. Calling the visibility methods shown in the preceding example can set their visibility on or off. Second, the horizontal progress bar defaults to a maximum progress value of 10,000. In the preceding example, we set it to 5,000, which is equivalent to 50 percent.When the value reaches the maximum value, the indicators fade away so that they aren’t visible.This happens for both indicators.

Formation et coursTélécharger le document complet

Télécharger aussi :

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *