Chapter 3. Layouts: Being a layout

image

We’ve only scratched the surface of using layouts.

So far, you’ve seen how to arrange views in a simple linear layout, but there’s so much more that layouts can do. In this chapter we’ll go a little deeper and show you how layouts really work. You’ll learn how to fine-tune your linear layouts. You’ll discover how to use frame layouts and scroll views. And by the end of the chapter, you’ll learn that even though they might look a little different, all layouts—and the views you add to them—have more in common than you might think.

It all starts with a layout

As you already know, layout files are written in XML, and they let you define what your app looks like.

Each time you write a layout, you need to do three things:

  1. Specify the type of layout.

    You tell Android how you want any views (such as buttons and text views) to be arranged by specifying a type of layout. A linear layout, for example, arranges views in a linear column or row, one after another.

    <LinearLayout ...>
    
    </LinearLayout>
  2. Specify the views.

    Each layout contains one or more views, which your app uses to display information or interact with the user.

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click me" />
  3. Tell an activity to use the layout.

    You tell Android which activity uses the layout you’ve just defined ...

Get Head First Android Development, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.