Why Does Android Use XML Isnt That Inefficient

Android is optimized for mobile devices with limited memory and horsepower, so you may find it strange that it uses XML so pervasively. After all, XML is a verbose, human-readable format not known for its brevity or efficiency, right?

Although you see XML when writing your program, the Eclipse plug-in invokes the Android resource compiler, aapt, to prepro-cess the XML into a compressed binary format. It is this format, not the original XML text, that is stored on the device.

Next we see a reference to <LinearLayout>: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation= "vertical" android:layout_width= "fill_parent" android:layout_height="fill_parent"> <!-- ... --> </LinearLayout>

A layout is a container for one or more child objects and a behavior to position them on the screen within the rectangle of the parent object. Here is a list of the most common layouts provided by Android:

• FrameLayout: Arranges its children so they all start at the top left of the screen. This is used for tabbed views and image switchers.

• LinearLayout: Arranges its children in a single column or row. This is the most common layout you will use.

• RelativeLayout: Arranges its children in relation to each other or to the parent. This is often used in forms.

• TableLayout: Arranges its children in rows and columns, similar to an HTML table.

Some parameters are common to all layouts:

xmlns:android=Mhttp://schemas.android.com/apk/res/androidM

Defines the XML namespace for Android. You should define this once, on the first XML tag in the file.

android:layout_width="filLparent", android:layout_height="filLparent"

Takes up the entire width and height of the parent (in this case, the window). Possible values are fill_parent and wrap_content.

Inside the <LinearLayout> tag you'll find one child widget:

<TextView android:layout_width= "fill_parent" android:layout_height= "'wrap_content" android:text="@string/hello" />

This defines a simple text label. Let's replace that with some different text and a few buttons. Here's our first attempt:

Download Sudokuv1/res/layout/mainl.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation= "vertical" android:layout_width= "fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width= "fill_parent" android:layout_height= "'wrap_content" android:text="@string/main_title" /> <Button android:layout_width= "fill_parent" android:layout_height= "'wrap_content" android:text="@string/continue_label" /> <Button android:layout_width= "fill_parent" android:layout_height= "'wrap_content" android:text="@string/new_game_label" /> <Button android:layout_width= "fill_parent" android:layout_height= "'wrap_content" android:text="@string/about_label" /> <Button android:layout_width= "fill_parent" android:layout_height= "'wrap_content" android:text="@string/exit_label" /> </LinearLayout>

If you see warnings in the editor about missing grammar constraints (DTD or XML schema), just ignore them. Instead of hard-coding English text into the layout file, we use the @string/resid syntax to refer to strings in the res/values/strings.xml file. You can have different versions of this and other resource files based on the locale or other parameters such as screen resolution and orientation.

Figure 3.3: First version of the opening screen

Open that file now, switch to the strings.xml tab at the bottom if necessary, and enter this:

Download Sudokuv1/res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?> <resources>

<string name="app_name">Sudoku</string> <string name="main_title">Android Sudoku</string> <string name="continue_label">Continue</string> <string name="new_game_label">New Game</string> <string name="about_label">About</string> <string name="exit_label">Exit</string> </resources>

Save strings.xml so Eclipse will rebuild the project. When you run the program now, you should see something like Figure 3.3.

Note: Because this is the third edition of the book, I have a pretty good idea where most people run into trouble. This is it, right here. You've made a lot of changes, so don't be surprised if you get an error mes sage instead of the opening screen. Don't panic; just skip ahead to Section 3.10, Debugging, on page 69 for advice on how to diagnose the problem. Usually a clue to the problem is waiting for you in the LogCat view. Sometimes selecting Project > Clean will fix things. If you're still stuck, drop by the book's web forum, and somebody would be happy to help you there.2

The current screen is readable, but it could use some cosmetic changes. Let's make the title text larger and centered, make the buttons smaller, and use a different background color. Here's the color definition, which you should put in res/values/colors.xml:

Download Sudokuv1/res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?> <resources>

<color name="background">#3500ffff</color> </resources>

And here's the new layout:

Download Sudokuv1/res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android= "'http://schemas.android.com/apk/res/android"

android:background="@color/background"

android:layout_height="fill_parent"

android:layout_width= "fill_parent"

android:padding="30dip"

android:orientation= "horizontal" >

<LinearLayout android:orientation= "vertical" android:layout_height= "'wrap_content" android:layout_width= "fill_parent" android:layout_gravity="center"> <TextView android:text="@string/main_title" android:layout_height= "'wrap_content" android:layout_width= "'wrap_content" android:layout_gravity= "'center" android:layout_marginBottom="25dip" android:textSize= "24.5sp" /> <Button android:id="@+id/continue_button" android:layout_width= "fill_parent" android:layout_height= "'wrap_content" android:text="@string/continue_label" />

2. http://forums.pragprog.com/forums/152

1:34 AM

Sudoku

Android Sudoku

Figure 3.4: Opening screen with new layout

<Button android:id= "@+id/new_button" android:layout_width= "fill_parent" android:layout_height= "'wrap_content" android:text="@string/new_game_label" /> <Button android:id="@+id/about_button" android:layout_width= "fill_parent" android:layout_height= "'wrap_content" android:text="@string/about_label" /> <Button android:id="@+id/exit_button" android:layout_width= "fill_parent" android:layout_height= "'wrap_content" android:text="@string/exit_label" /> </LinearLayout> </LinearLayout>

0 0

Post a comment