Developing the User Interface

Okay, it's time to start developing your user interface.

As stated previously, views in Android are the basic building blocks for user interface components. Anytime you implement a user interface component, such as a Layout, TextView, and so on, in the Android system, you are using a view.

So that Android knows how to lay out your view on the screen, however, you must configure a couple of attributes. These attributes, known in the Android SDK as LayoutParams, are

  • The layout_width attribute, which specifies the view's width
  • The layout_height attribute, which specifies the height

If you're using a static layout, these two attributes must be set in the XML layout. If you're creating views dynamically through code, the layout parameters must be set through Java code. Either way, you cannot do without them. I do not cover dynamic creation of views in this book. For more about dynamic creation of views, refer to the API samples that come with the Android SDK.

image If you forget to provide values for layout_width or layout_height, your Android application will crash when rendering the view. Thankfully, you are reminded of this sort of omission quickly when you test your application.

These attributes accept any pixel value or density-independent pixel value (density-independent pixels will be explained in Chapter 9) to specify their respective dimensions. However, two of the most common values for layout_width and layout_height don't specify pixel values at all. They are

  • The fill_parent value, which instructs the Android system to fill as much space as possible on the screen based on the available space of the parent layout.

    image As of Android 2.2, fill_parent has been renamed to match_parent. However, to maintain backward compatibility, fill_parent is still supported. If you plan on developing for Android 2.2 and above, use match_parent. If you need to support devices prior to Android 2.2, then use fill_parent.

  • The wrap_content value, which instructs the Android system to take up only as much space as needed to show the view. As the view's contents grow, as would happen with a TextView, the view's viewable space grows. This feature is similar to the Autosize property in Windows forms development.

To define these attributes in your Screen Brightness Toggle app, first return to the XML view of your layout by clicking the main.xml tab, which is located directly next to the Graphical Layout tab you clicked to get to the visual designer. When you are in the XML view, replace the contents of the view — that is, all the XML code within the main.xml file — with the following:

<?xml version=“1.0” encoding=“utf-8”?>
<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android” →2
    android:layout_width=“match_parent”                                    →3
    android:layout_height=“match_parent”                                   →4
    >

</RelativeLayout>

Let me explain the Android layout XML that you just added into your file:

→2 xmlns:android=“…” defines the XML namespace that you will use to reference part of the Android SDK.
→3 This line is the first attribute definition. This line informs the view that it should fill as much horizontal space as it can, up to its parent. In short, it should make the width as wide as it can be within the parent.
→4 Similarly, this line informs the view that it should fill as much vertical space as it can, up to its parent — it should make the height as tall as it can be within the parent.

At this point, you have defined your layout to fill the entire screen by setting both the width and height to “match_parent”.

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

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