• 沒有找到結果。

and higher applications on older platforms

Dividing the screen into fragments

Running 3.0 and higher applications on older platforms

The advent of Android 3.0 introduced some significant new features, aimed in particular at tablet devices. However, there are a large number of Android tablets that run on earlier platforms and so Google produced a compatibility package so that most of these new features would run on older versions, going back as far as Android 1.6 (API level 4).

This chapter has introduced a lot of new concepts but we can conclude it with a nice and easy example showing how to install the compatibility library and how to include packages from it, so that we can take advantage of these wonderful new features on older platforms.

Getting ready

There is a strong chance that you have already downloaded and installed the Android Compatibility Package during a regular software update of your SDK. All the same, open the AVD manager, select Installed packages, and check that you have; it will look like this:

How to do it....

1. Having checked that the Compatibility package is installed, start up a new Android

2. Open the project properties, which can be done with Alt + Enter on a PC, and select Java Build Path followed by the Libraries tab.

3. Click on the Add External JARs... button and browse to the extras folder in your android-sdk folder. Find the android-support-v4.jar file and click on OK.

4. There should now be a new folder in your project called Referenced Libraries containing the library:

5. Change the declaration of your main Java Activity file from extends Activity to extends FragmentActivity and press Shift + Ctrl + O to include the line import android.support.v4.app.FragmentActivity; into your project.

How it works...

There is not much to explain when it comes to how this works. All we have done is import a new library so that we can use features introduced in 3.0 on earlier platforms. However, this is a remarkably useful function as it allows us to publish applications that will run on the widest possible range of devices.

The Android layout classes we have covered here allow us to set out our screens in a variety of useful ways. Of course it is the objects that we place inside these containers that provide their real functionality and it is these widgets that we will cover in the next chapter.

Widgets 3

In this chapter, we will cover the following topics:

f Inserting a widget into a layout

f Adding images to a widget

f Creating a widget at runtime

f Applying a style

f Turning a style into a theme

f Using a platform style or theme

f Creating a custom component

Introduction

Many of the individual on-screen components that we see in an Android application are provided by the android:widget package. It provides dozens of classes and interfaces for creating and using such objects. The system also allows us to extend the base android.view.

View class to create custom widgets of our own.

The Android Widget package provides us with a wide variety of purpose-built components such as text views, date pickers, rating bars, and all kinds of other familiar UI elements. In addition, many widgets have associated interfaces such as the list adapters that we saw in the previous chapter.

It is worth making the distinction here, between Widgets, which are descended from the base View class, and AppWidgets, which are mini applications that can be embedded into an activity.

Many widgets can have images, sounds, and other media connected to them and these, along with most other properties, can be set and changed with static XML files or dynamically with Java code.

Our applications can be easily given a consistent look across different screens with the use of styles, which can, in turn, be encapsulated in a theme and applied to a wider scope of objects such as whole activities or even applications.

Another powerful feature of the Android UI is the ability to construct our own customized widgets, which can be done by extending the View class itself. These classes can again be reused and like built-in widgets, bound to data.

Inserting a widget into a layout

Android widgets are included into a layout just as the views and view groups that we have been previously using. Although each widget has its own unique characteristics, they all inherit from the View class.

Here we will insert a CheckBox widget but use a callback from the parent class to edit its properties.

Getting ready

Start up a new Android project in Eclipse and open the main.xml file in Graphical Layout mode.

How to do it...

1. Drag-and-drop a CheckBox from the Form Widget palette onto the default layout, as seen in the next screenshot:

2. Open the main.xml tab and edit the new element so that it has the following properties:

<CheckBox

android:text="A CheckBox"

android:id="@+id/check_box"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

3. Include the following code inside the main Java activity's onCreate() method:

CheckBox checkBox = (CheckBox) findViewById(R.id.check_box);

checkBox.setOnClickListener(new OnClickListener() { public void onClick(View v) {

if (((CheckBox) v).isChecked()) {

((CheckBox) v).setText("selected");

} else {

((CheckBox) v).setText("not selected");

} } });

4. To import the necessary libraries, from Eclipse, press Shift + Ctrl + O. This will automatically add the following lines to the code:

import android.view.View.OnClickListener;

import android.widget.CheckBox;

5. Run the project on an emulator or handset.

How it works...

Widgets, like views, can be given a resource ID as part of their XML definition. Likewise, the findViewById() method can be used to associate a widget with a Java variable.

Most widgets lie at the bottom or close to the bottom of a hierarchy of classes. The CheckBox, for example, is descended from the CompoundButton, which in turn is inherited from the Button and View objects. The OnClickListener interface is defined in the View class, and so takes its argument as a View. This means that we have to cast the widget as a CheckBox so as to be able to use the methods not defined in the parent such as setText() and isChecked().

There's more...

Checkboxes and other widgets descended from the CompoundButton class will change their states automatically when clicked, but it is possible to change this through code if we wish.

Changing a CheckBox's state with code

The CheckBox handles its own graphical state automatically, that is, it will toggle between the selected and unselected states without any extra code. If we need to change the state programmatically, we can use the CompoundButton's setChecked() method, which takes a Boolean argument to change the checkbox's state.

See also

To learn how to manage checkable menu items refer to the recipe Building menu groups of checkable items in Chapter 4, Menus.