Using Layouts

The preferred way to implement layouts is by using XML as external resources. A layout XML must contain a single root element. This root node can contain as many nested layouts and Views as necessary to construct an arbitrarily complex screen.

Listing 4-3 shows a simple layout that places a TextView above an EditText control using a vertical LinearLayout.

^^ LISTING 4-3: Simple Linear Layout in XML Available for download on <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: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="Enter Text Below"

<EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Text Goes Here!"


Note that for each of the layout elements, the constants wrap_content and fill_parent are used rather than an exact height or width in pixels. These constants are the simplest, and most powerful, technique for ensuring your layouts are screen-size and resolution independent.

The wrap_content constant will set the size of a View to the minimum required to contain the contents it displays (such as the height required to display a wrapped text string). The fill_parent constant expands the View to fill the available space within the parent View (or screen).

In Listing 4-3, the layout is set to fill the entire screen, while both text-based Views are asked to fill the full available width. Their height is restricted to that required by the text being displayed.

Later in this chapter you'll learn how to set the minimum height and width for your own controls, as well as further best practices for resolution independence.

Implementing layouts in XML decouples the presentation layer from the View and Activity code. It also lets you create hardware-specific variations that are dynamically loaded without requiring code changes.

When preferred, or required, you can implement layouts in code. When you're assigning Views to layouts in code, it's important to apply LayoutParameters using the setLayoutParams method, or by passing them in to the addView call, as shown in Listing 4-4.

LISTING 4-4: Simple LinearLayout in code Available for download on LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL);

TextView myTextView = new TextView(this); EditText myEditText = new EditText(this);

myTextview.setText("Enter Text Below"); myEditText.setText("Text Goes Here!");

int lHeight = LinearLayout.LayoutParams.FILL_PARENT; int lWidth = LinearLayout.LayoutParams.WRAP_CONTENT;

ll.addview(myTextview, new LinearLayout.LayoutParams(lHeight, lWidth)); ll.addview(myEditText, new LinearLayout.LayoutParams(lHeight, lWidth)); setContentview(ll);

Was this article helpful?

0 0
Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide

Post a comment