Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

5. Reusable UI > Abstracting Your Layouts

Abstracting Your Layouts

Reusing components is one of the hallmarks of good object-oriented design, and Android supports abstracting your views to provide a similar level of componentization. By separating your UI into discrete components, you can reuse them throughout your app. This provides your app with a consistent look and makes design changes much easier to implement.

The <Include> Tag

Android provides a simple method for including one layout inside another: the <include> tag. Using this tag, a different layout can be included in your view hierarchy just as if it had been written in the original XML. This makes adding reusable components to your UI a snap. Here is an example of the <include> tag:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <include layout="@layout/sub_layout" />
</LinearLayout>


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial