Search Suggest

Welcome to Wonderland!!

Android Hacks for Developers - Using lazy loading and avoiding replication Android v1.6+

4 min read

Using lazy loading and avoiding replication Android v1.6+

When you’re creating complex layouts, you may find yourself adding a lot of View- Groups and Views. But making your view hierarchy tree taller will also make it slower. Creating optimized layouts is fundamental to building an application that runs fast and is responsive to the user. In this hack, you’ll learn how to use the <include /> tag in your XML to avoid replication, and how to use the View Stub class to lazy load views.

Avoid replication using the <include /> tag

Let’s imagine we want to add a footer to every view in our application—something simple, such as a TextView with our application’s name. If we have more than one Activity, we might have more than one XML file. Would we copy this TextView to every XML file? What happens if we need to edit it in the future? Copying and pasting would solve the problem, but it doesn’t sound efficient. The easiest way to add a footer to our application is to use the <include /> tag. Let’s look at how it can help us out. We use the <include /> tag in XML to add another layout from another XML file.
 

In our example, we’ll create our complete view, and at the bottom we’ll add the <include /> tag pointing to our footer’s layout.

<RelativeLayout

xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:text="@string/hello"/>
<include layout="@layout/footer_with_layout_properties"/>
</RelativeLayout/>

And the footer_with_layout_properties would look like the following:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"

android:gravity="center_horizontal"
android:text="@string/footer_text"/>

In this first example, we’ve used the <include /> tag with the only required layout. You might be thinking, “OK, this works because we’re using a RelativeLayout for our main XML. What’ll happen if one of the XML files is a LinearLayout? android layout_alignParentBottom="true" wouldn’t work because it’s a RelativeLayout attribute.” That’s true. Let’s look at the second way to use includes, where we’ll place android:layout_* attributes in the <include /> itself. 

Lazy loading views with the ViewStub class

When designing your layouts, you may have thought about showing a view depending on the context or the user interactions. If you’ve ever found yourself making a view invisible and then making it visible afterward, you should keep on reading—you’ll want to use the ViewStub class. 
 
A ViewStub is an invisible, zero-sized View that can be used to lazily inflate layout resources at runtime. When a ViewStub is made visible, or when inflate() is invoked, the layout resource is inflated. The ViewStub then replaces itself in its parent with the inflated View or Views. You already know what a ViewStub is, so let’s see what you can do with it. In the following example you’ll use a ViewStub to lazy load a MapView. Imagine creating a view with the details about a place. Let’s look at two possible scenarios:
 
1. Some venues don’t have GPS information
2. The user might not need the map
 
 
 

Rate this article

Siyalive CSC DigitalSeva Kunnamkulam, Common Service Centres Scheme (CSC), Under Ministry of Electronics & Information Technology, Govt. of India

You may like these posts

Post a Comment