• No se han encontrado resultados

E NTRE RISAS Y SEMEJANZAS

In document Las Aventuras Matematicas de Daniel (página 119-123)

Most widget toolkits offer one or more grid-based controls. Android has a GridView control that can display data in the form of a grid. Note that although we use the term “data” here, the contents of the grid can be text, images, and so on.

The GridView control displays information in a grid. The usage pattern for the GridView is to define the grid in the XML layout (see Listing 4–20), and then bind the data to the grid using an android.widget.ListAdapter. Don’t forget to add the uses-permission tag to the AndroidManifest.xml file to make this example work.

Listing 4–20. Definition of a GridView in an XML Layout and Associated Java Code <?xml version="1.0" encoding="utf-8"?>

<!-- This file is at /res/layout/gridview.xml -->

<GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/dataGrid" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10px" android:verticalSpacing="10px" android:horizontalSpacing="10px" android:numColumns="auto_fit" android:columnWidth="100px" android:stretchMode="columnWidth" android:gravity="center" />

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

setContentView(R.layout.gridview); GridView gv = (GridView)this.findViewById(R.id.dataGrid); Cursor c = getContentResolver().query(People.CONTENT_URI, null, null, null, null);

startManagingCursor(c);

String[] cols = new String[]{People.NAME}; int[] names = new int[]{android.R.id.text1};

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1 ,c,cols,names);

gv.setAdapter(adapter); }

Listing 4–20 defines a simple GridView in an XML layout. The grid is then loaded into the

activity’s content view. The generated UI is shown in Figure 4–9.

Figure 4–9. A GridView populated with contact information

The grid shown in Figure 4–9 displays the names of the contacts on the device. We have decided to show a TextView with the contact names, but you could easily generate a

grid filled with images and the like. In fact, we’ve used another shortcut in this example. Instead of creating our own layout file for the grid items, we’ve taken advantage of predefined layouts in Android. Notice the prefix on the resources for the grid item layout and the grid item field is android:. Instead of looking in our local /res directory, Android

and looking under platforms/<android-version>/data/res/layout. You’ll find simple_list_item_1.xml there and can see inside that it defines a simple TextView

whose android:id is @android:id/text1. That’s why we specified android.R.id.text1

for the names ID for the cursor adapter.

The interesting thing about the GridView is that the adapter used by the grid is a

ListAdapter. Lists are generally one-dimensional, whereas grids are two-dimensional. What we can conclude, then, is that the grid actually displays list-oriented data. In fact, if

you call getSelection(), you get back an integer representing the index of the selected

item. Likewise, to set a selection in the grid, you call setSelection() with the index of

the item you want selected.

Date and Time Controls

Date and time controls are quite common in many widget toolkits. Android offers several date- and time-based controls, some of which we’ll discuss in this section. Specifically,

we are going to introduce the DatePicker, the TimePicker, the AnalogClock, and the

DigitalClock controls.

The DatePicker and TimePicker Controls

As the names suggest, you use the DatePicker control to select a date and the

TimePicker control to pick a time. Listing 4–21 and Figure 4–10 show examples of these controls.

Listing 4–21. The DatePicker and TimePicker Controls in XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <DatePicker android:id="@+id/datePicker" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TimePicker android:id="@+id/timePicker" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>

Figure 4–10. The DatePicker and TimePicker UIs

If you look at the XML layout, you can see that defining these controls is quite easy. The user interface, however, looks a bit overdone. Both controls seem oversized, but for a mobile device, you can’t argue with the look and feel.

As with any other control in the Android toolkit, you can access the controls

programmatically to initialize them or to retrieve data from them. For example, you can initialize these controls as shown in Listing 4–22.

Listing 4–22. Initializing the DatePicker and TimePicker with Date and Time, Respectively protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState); setContentView(R.layout.datetime); DatePicker dp = (DatePicker)this.findViewById(R.id.datePicker); dp.init(2008, 11, 10, null); TimePicker tp = (TimePicker)this.findViewById(R.id.timePicker); tp.setIs24HourView(true); tp.setCurrentHour(new Integer(10)); tp.setCurrentMinute(new Integer(10)); }

Listing 4–22 sets the date on the DatePicker to November 10, 2008. Similarly, the

view. If you do not set values for these controls, the default values will be the current date and time as known to the device.

Finally, note that Android offers versions of these controls as modal windows, such as

DatePickerDialog and TimePickerDialog. These controls are useful if you want to

display the control to the user and force the user to make a selection. We’ll cover dialogs in more detail in Chapter 5.

The AnalogClock and DigitalClock Controls

Android also offers an AnalogClock and a DigitalClock (see Figure 4–11).

Figure 4–11. Using the AnalogClock and DigitalClock

As shown, the analog clock in Android is a two-handed clock, with one hand for the hour indicator and the other hand for the minute indicator. The digital clock supports seconds in addition to hours and minutes.

These two controls are not that interesting because they don’t let you modify the date or time. In other words, they are merely clocks whose only capability is to display the current time. Thus, if you want to change the date or time, you’ll need to stick to the

DatePicker/TimePicker or DatePickerDialog/TimePickerDialog.

Other Interesting Controls in Android

The controls that we have discussed so far are fundamental to any Android application. In addition to these, Android also offers a few other interesting controls. We’ll briefly introduce these other controls in this section.

The MapView Control

The com.google.android.maps.MapView control can display a map. You can instantiate this control either via XML layout or code, but the activity that uses it must extend MapActivity. MapActivity takes care of multithreading requests to load a map, perform caching, and so on.

Listing 4–23 shows an example instantiation of a MapView.

Listing 4–23. Creating a MapView Control via XML Layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <com.google.android.maps.MapView android:layout_width="fill_parent" android:layout_height="fill_parent" android:enabled="true" android:clickable="true" android:apiKey="myAPIKey" /> </LinearLayout>

We’ll discuss the MapView control in detail in Chapter 7, when we discuss location-based

services. This is also where you’ll learn how to obtain your own mapping API key.

The Gallery Control

The Gallery control is a horizontally scrollable list control that always focuses at the center of the list. This control generally functions as a photo gallery in touch mode. You

can instantiate a Gallery either via XML layout or code:

<Gallery

android:id="@+id/galleryCtrl" android:layout_width="fill_parent" android:layout_height="wrap_content" />

Using the Gallery control is similar to using a list control. That is to say, you get a

reference to the gallery, then call the setAdapter() method to populate data, then

register for on-selected events.

The Spinner Control

The Spinner control is like a dropdown menu. You can instantiate a Spinner either via XML layout or code: <Spinner android:id="@+id/spinner" android:layout_width="wrap_content" android:layout_height="wrap_content" />

Using the Spinner control is also similar to using a list control. That is to say, you get a reference to the spinner, then call the setAdapter() method to populate data, then register for on-selected events. We’ll use Spinner as an example in the section later in this chapter called “Getting to Know ArrayAdapter”.

This concludes our discussion of the Android control set. As we mentioned in the beginning of the chapter, building user interfaces in Android requires you to master two things: the control set and the layout managers. In the next section, we are going to discuss the Android layout managers.

In document Las Aventuras Matematicas de Daniel (página 119-123)