Adding Widgets to Your Layout

A widget is a View object that serves as an interface for interaction with the user. Android devices come fully equipped with various widgets, including buttons, check boxes, and text-entry fields, so that you can quickly build your user interface. Some widgets are more complex, such as a date picker, a clock, and zoom controls.

Widgets also provide user interface events that inform you when a user has interacted with the particular widget, such as tapping a button.

image The Android documentation can get a bit sticky at times, and widgets and app widgets are regularly confused. They are two completely different topics. I am currently referring to widgets in the sense that you can find defined at http://d.android.com/reference/android/widget/package-summary.html. Please note, these are not the same as app widgets, which I will cover in Chapter 8.

For your Screen Brightness Toggle app, you need to add a single widget: the ToggleButton widget, which lets the user toggle the screen brightness of the tablet.

To add a ToggleButton to your layout, type the following code into your main.xml file, just before the </RelativeLayout> line:

<ToggleButton
    android:id=“@+id/toggleButton”                                  →2
    android:layout_width=“wrap_content”                             →3
    android:layout_height=“wrap_content”                            →4
    android:layout_centerInParent=“true”                            →5
    android:textOn=“Dimmer On”                                      →6
    android:textOff=“Dimmer Off”/>                                  →7

The ToggleButton contains some new parameters:

→2 The id attribute defines the unique identifier for the view in the Android system. Here you inform Android that the identifier for your widget is toggleButton, which means you can refer to it by that name in the Java code later. Nothing beats the actual Android documentation on the subject, which is located at http://developer.android.com/guide/topics/ui/declaring-layout.html.
→3 to →4 The height and width are set to wrap_content, which informs the Android layout system to place the widget on the screen and only take up as much usable space as it needs.
→5 Because you've placed the ToggleButton code within the RelativeLayout, the layout_centerInParent attribute gets inherited from RelativeLayout. This attribute instructs the Android system to position the ToggleButton within the center of its parent (that is, within the RelativeLayout).
→6 This attribute defines the ToggleButton's text value when the ToggleButton is in its On state.
→7 Similarly, this attribute defines the ToggleButton's text value when the ToggleButton is in its Off state.

You have now added a button to your view with an ID resource of toggleButton. The ID is how you will reference the button in the Java code (which I get to in Chapter 8).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.91.254