Using Custom Array Adapter on Android List View

List View is an element that displays a collection of items in single column direction. It has an internal vertical scroll bar that enable user to scroll if it’s height bigger than display height. The type of single item in the list is any of java object.

[cci]ListView[/cci] needs an adapter to works. This adapter behaves as a data resources for the list. The adapter also defines how each items in the list is displayed. The adapter is attached to the list via [cci]setAdapter[/cci] method on the [cci]ListView[/cci] object. There are two commons used adapters:

  • Array Adapter: An adapter that is designed to work with arrays data resources.
  • Cursor Adapter: An adapter that is designed to handle database related data.

This tutorial will focuses on array adapter.

Simple Array Adapter

A simple array adapter is an array adapter that is built using array of string as items. The array of string is passed to the constructor of [cci]ArrayAdapter[/cci] object.

List<String> apps = new ArrayList<String>();
apps.add("Twitter");
apps.add("Whatsapp");
apps.add("Facebook");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, apps);

The constructor of array adapter have 3 arguments:

  1. context: this is the application context
  2. layout resource id: the layout resource id for item view
  3. array items: this is array object for items

We will attach the created adapter into the list by using [cci]setAdapter[/cci] method on [cci]ListView[/cci] object.

List Activity

[cci]ListActivity[/cci] is a subclass of [cci]Activity[/cci] that will make our work with [cci]ListView[/cci] easier. [cci]ListActivity[/cci] has internal implementation method that is related to [cci]ListView[/cci]. You must have list view object with id is [cci]android:id/list[/cci] in the layout file to use List Activity.

<ListView
   android:id="@android:id/list"
   android:layout_width="match_parent"
   android:layout_height="wrap_content" >
</ListView>

Now we will create a sample list view application using simple array adapter. Open your Eclipse IDE and create new android project by selecting menu: File -> New -> Android Application Project. Fill out required fields.

  1. Create new xml file: [cci]activity_main.xml[/cci]
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </ListView>
    
    </LinearLayout>
    
  2. Create new activity class: [cci]MainActivity.java[/cci]
    package com.sj.customlistview;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import android.app.ListActivity;
    import android.os.Bundle;
    import android.widget.ArrayAdapter;
    
    public class MainActivity extends ListActivity{    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);        
            setContentView(R.layout.activity_main);        
            initView();   
        }
    
        private void initView() {
            List<String> apps = new ArrayList<String>();
            apps.add("Twitter");
            apps.add("Whatsapp");
            apps.add("Facebook");
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, apps);
            setListAdapter(adapter);
        }
    }
    
  3. Put your activity in [cci]AndroidManifest.xml[/cci] file
  4. <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.sj.customlistview"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="17" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.sj.customlistview.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    

Run your project, your list view will be displayed like image below:

Simple Android List View
Simple Android List View


Customize Array Adapter

By default, array adapter displays a [cci]TextView[/cci] for each items and set the text of it by calling [cci]toString[/cci] method of item object. We can customize the default display of array adapter by overriding [cci]getView[/cci] method.
For a demo, we will modify previous project and use custom array adapter:

  1. Create new java class: [cci]Application.java[/cci].
    This class represents a single object of list item.

    package com.sj.customlistview;
    
    public class Application {
        private String title;
        private long totalDl;
        private int rating;
        private String icon;
        
        public String getTitle() {
            return title;
        }
        public void setTitle(String title) {
            this.title = title;
        }
        public long getTotalDl() {
            return totalDl;
        }
        public void setTotalDl(long totalDl) {
            this.totalDl = totalDl;
        }
        public int getRating() {
            return rating;
        }
        public void setRating(int rating) {
            this.rating = rating;
        }
        public String getIcon() {
            return icon;
        }
        public void setIcon(String icon) {
            this.icon = icon;
        }
    }
    
  2. Create new xml file: [cci]app_custom_list.xml[/cci]
    This file defines a view for list item

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <ImageView
            android:id="@+id/appIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"        
            android:layout_marginLeft="3dp"
            android:layout_marginTop="3dp"
            android:src="@drawable/facebook" />
        <TextView
            android:id="@+id/titleTxt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@id/appIcon"
            android:layout_toRightOf="@id/appIcon"
            android:layout_marginLeft="3dp"
            android:text="Application Title"
            android:textSize="22dp"/>
    
        <LinearLayout
            android:id="@+id/ratingCntr"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/appIcon"
            android:layout_below="@id/titleTxt"
            android:layout_marginLeft="5dp" >
        </LinearLayout>
    
        <TextView
            android:id="@+id/dlTxt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/titleTxt"
            android:layout_alignParentRight="true"
            android:layout_marginRight="3dp"
            android:text="0 dl" />    
    </RelativeLayout>
    
  3. Create new java class: [cci]AppAdapter.java[/cci]
    This is our adapter that will inflate [cci]app_custom_list.xml[/cci] and set the data into it.

    package com.sj.customlistview;
    
    import java.text.NumberFormat;
    import java.util.List;
    
    import android.content.Context;
    import android.content.res.Resources;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    
    public class AppAdapter extends ArrayAdapter<Application>{
        private List<Application> items;
        
        public AppAdapter(Context context, List<Application> items) {
            super(context, R.layout.app_custom_list, items);
            this.items = items;
        }
        
        @Override
        public int getCount() {
            return items.size();
        }
        
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            
            if(v == null) {
                LayoutInflater li = LayoutInflater.from(getContext());
                v = li.inflate(R.layout.app_custom_list, null);            
            }
            
            Application app = items.get(position);
            
            if(app != null) {
                ImageView icon = (ImageView)v.findViewById(R.id.appIcon);
                TextView titleText = (TextView)v.findViewById(R.id.titleTxt);
                LinearLayout ratingCntr = (LinearLayout)v.findViewById(R.id.ratingCntr);
                TextView dlText = (TextView)v.findViewById(R.id.dlTxt);
                
                if(icon != null) {
                    Resources res = getContext().getResources();
                    String sIcon = "com.sj.customlistview:drawable/" + app.getIcon();
                    icon.setImageDrawable(res.getDrawable(res.getIdentifier(sIcon, null, null)));
                }
                
                if(titleText != null) titleText.setText(app.getTitle());
                
                if(dlText != null) {
                    NumberFormat nf = NumberFormat.getNumberInstance();
                    dlText.setText(nf.format(app.getTotalDl())+" dl");            
                }
                
                if(ratingCntr != null && ratingCntr.getChildCount() == 0) {        
                    /*
                     * max rating: 5
                     */
                    for(int i=1; i<=5; i++) {
                        ImageView iv = new ImageView(getContext());
                        
                        if(i <= app.getRating()) {
                            iv.setImageDrawable(getContext().getResources().getDrawable(R.drawable.start_checked));
                        }
                        else {                
                            iv.setImageDrawable(getContext().getResources().getDrawable(R.drawable.start_unchecked));
                        }
                        
                        ratingCntr.addView(iv);
                    }
                }
            }
            
            return v;
        }
    }
    
  4. Open your [cci]MainActivity.java[/cci] and modify several lines.
    package com.sj.customlistview;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import android.app.ListActivity;
    import android.os.Bundle;
    
    public class MainActivity extends ListActivity{    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);        
            setContentView(R.layout.activity_main);        
            initView();   
        }
    
        private void initView() {
            List<Application> apps = populateSampleApplication();
            AppAdapter adapter = new AppAdapter(this, apps);
            setListAdapter(adapter);        
        }
        
        private List<Application> populateSampleApplication(){        
            String[] apps = new String[] {                
                "Twitter,450000,5,twitter",
                "Skype,300002,2,skype",
                "Facebook,500560,4,facebook"
            };
            
            List<Application> list = new ArrayList<Application>();
            
            for(String app:apps) {
                String[] rApp = app.split(","); 
                Application ap = new Application();
                ap.setTitle(rApp[0]);
                ap.setTotalDl(Integer.parseInt(rApp[1]));
                ap.setRating(Integer.parseInt(rApp[2]));
                ap.setIcon(rApp[3]);
                list.add(ap);
            }
            
            return list;
        }
    }
    

I’ve used some social icons in this project. You can download the icons here: social icons
Now run your project, it will display list view below:

Custom Array Adapter
Custom Array Adapter

Android Gravity and Layout Gravity

Gravity is an android method for aligning view in a layout. There are two kinds of gravity, gravity and layout gravity. Basically a gravity is set in xml layout files, but you can also set a gravity of any view in java source code. To set gravity in xml use [cci]android:layout_gravity[/cci] and [cci]android:gravity[/cci] attributes. The values of both attributes is combination of the following constants:

top : Push object to the top of its container, not changing its size.
bottom : Push object to the bottom of its container, not changing its size.
left : Push object to the left of its container, not changing its size.
right : Push object to the right of its container, not changing its size.
center_vertical : Place object in the vertical center of its container, not changing its size.
fill_vertical : Grow the vertical size of the object if needed so it completely fills its container.
center_horizontal : Place object in the horizontal center of its container, not changing its size.
fill_horizontal : Grow the horizontal size of the object if needed so it completely fills its container.
center : Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.
fill : Grow the horizontal and vertical size of the object if needed so it completely fills its container.
clip_vertical : Additional option that can be set to have the top and/or bottom edges of the child clipped to its container’s bounds. The clip will be based on the vertical gravity: a top gravity will clip the bottom edge, a bottom gravity will clip the top edge, and neither will clip both edges.
clip_horizontal : Additional option that can be set to have the left and/or right edges of the child clipped to its container’s bounds. The clip will be based on the horizontal gravity: a left gravity will clip the right edge, a right gravity will clip the left edge, and neither will clip both edges.
start : Push object to the beginning of its container, not changing its size.
end : Push object to the end of its container, not changing its size.

android:gravity attribute

[cci]android:gravity[/cci] will set alignment to the content of a view. This aligment will only take effect if size of view is bigger than view’s content. Maybe you got confuse, what is the different between view and view content. Consider a button view below.

Button View
Button View

As you can see from image above, the button view is the rectangle boundary of a button, while the content of the button view is the text “button” it self.

Now, we will align the content of the button to bottom right of the button. To align content to bottom right we add [cci]android:gravity=”bottom|right”[/cci] attribute.

<?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" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:text="Button"
        android:gravity="bottom|right" />

</LinearLayout>

Will result as follow:

Content of button view aligned to bottom right
Content of button view aligned to bottom right

android:layout_gravity attribute

[cci]android:layout_gravity[/cci] is used to align view child to its parent. Note that this attribute will only take effect if the parent is [cci]LinearLayout[/cci] and the size of child smaller than parent size.


Now we will align a button to the right of parent. To do this, we must add [cci]android:layout_gravity=”right”[/cci] attribute.

<?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" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_gravity="right" />

</LinearLayout>

The result is as follow:

Button view is aligned to right
Button view is aligned to right

Advance Android Relative Layout Example

Relative Layout is an android view group to align all childrens relative to other view or its parent. Relative layout give you flexibiliy to place childs wherever you want. In the previous example, I have give some basic example of Relative Layout. Now, I will give you how to create a more complex relative layout example.

Our goal is to create layout like this:

Advance relative layout example
Advance relative layout example

See the following step by step to create above layout:

1. Aligning child to top left of parent

Actually, by default every childs is aligned to top left of parent in relative layout. So we needn’t add any special attribute to align chilc in top left of parent. But you can align child to top left of parent by adding [cci]android:layout_alignParentLeft=”true”[/cci] and [cci]android:layout_alignParentTop=”true”[/cci] attributes.

<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

2. Aligning child in top right of parent

To align child in top right of parent, add the following attributes [cci]android:layout_alignParentRight=”true”[/cci] and [cci]android:layout_alignParentTop=”true”[/cci].

<Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button2"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" />

3. Aligning child in bottom left of parent

To align child in bottom left of parent, add the following attributes [cci]android:layout_alignParentBottom=”true”[/cci] and [cci]android:layout_alignParentLeft=”true”[/cci].

<Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button2"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true" />

4. Aligning child in bottom right of parent

To align child in bottom right of parent, add the following attributes [cci]android:layout_alignParentBottom=”true”[/cci] and [cci]android:layout_alignParentRight=”true”[/cci].

<Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button2"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true" />

5. Align child in the center of parent

This is our center child and you must give it an [cci]id[/cci] as our other 4 childs aligned relative this child. To align a child center of the parent, add the following attribute [cci]android:layout_centerInParent=”true”[/cci]. This attribute will align our child center to the parent, vertically and horizontally.

<Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button5"
        android:layout_centerInParent="true"/>

6. Align child in top left of the center child

To align child in top left of the center child, add the following attributes [cci]android:layout_toLeftOf=”@id/button5″[/cci] and [cci]android:layout_above=”@id/button5″[/cci].

<Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button6"
        android:layout_toLeftOf="@id/button5"
        android:layout_above="@id/button5"/>


7. Align child in top right of the center child

To align child in top right of the center child, add the following attributes [cci]android:layout_toRightOf=”@id/button5″[/cci] and [cci]android:layout_above=”@id/button5″[/cci].

<Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button6"
        android:layout_toRightOf="@id/button5"
        android:layout_above="@id/button5"/>

8. Align child in bottom right of the center child

To align child in bottom right of the center child, add the following attributes [cci]android:layout_toRightOf=”@id/button5″[/cci] and [cci]android:layout_below=”@id/button5″[/cci].

<Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button6"
        android:layout_toRightOf="@id/button5"
        android:layout_below="@id/button5"/>

9. Align child in bottom left of the center child

To align child in bottom left of the center child, add the following attributes [cci]android:layout_toLeftOf=”@id/button5″[/cci] and [cci]android:layout_below=”@id/button5″[/cci].

<Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button6"
        android:layout_toLeftOf="@id/button5"
        android:layout_below="@id/button5"/>

Final Source Codes

Below is the final source codes:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button2"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button3"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true" />
    
    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button4"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true" />
    
    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button5"
        android:layout_centerInParent="true"/>
    
    <Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button6"
        android:layout_toLeftOf="@id/button5"
        android:layout_above="@id/button5"/>
    <Button
        android:id="@+id/button7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button7"
        android:layout_toRightOf="@id/button5"
        android:layout_above="@id/button5"/>
    <Button
        android:id="@+id/button8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button8"
        android:layout_toRightOf="@id/button5"
        android:layout_below="@id/button5"/>
    <Button
        android:id="@+id/button9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button9"
        android:layout_toLeftOf="@id/button5"
        android:layout_below="@id/button5"/>
</RelativeLayout>

Android Linear Layout Example

Linear Layout is an android View Group that aligns all children in single oriented manner, vertically or horizontally. A Linear layout can have only one orientation, only vertical or only horizontal. The orientation can be specified by using attribute [cci]android:orientation[/cci].

Horizontal Orientation

By default, if you don’t specify the attribute [cci]android:orientation[/cci], the orientation of linear layout will be horizontal. But you can put [cci]android:orientation=”horizontal”[/cci] to set horizontal orientation. For example:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
         />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world1"
         />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world2"
         />
</LinearLayout>

The result of above layout is:

Linear layout example
Linear layout example

Vertical Orientation

To set the orieantation of linear layout to vertical, add [cci]android:orientation=”vertical”[/cci] to the View Group. For example:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
         />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world1"
         />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world2"
         />
</LinearLayout>

The result of above layout is:

Linear layout example
Linear layout example


Flexible Width or Hight of Children

Linear Layout support flexibility of width or height for its child to consume remaining space in the parent. Flexible width only supported in horizontal orientation and flexible height only supported in vertical orietation. Use [cci]android:layout_weight[/cci] to make a child flexible. The value of the attribute is an integer value. The larger number of [cci]android:layout_weight[/cci] of a child, it consumes more extra space in the parent. Default value of the attributes in 0.

Flexible Width in Horizontal Orientation

To make width of a child flexible, you must set it width to [cci]0dp[/cci] and layout_weight greater than 0.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button3" />

         />
</LinearLayout>

Here is the result:

Layout weight example
Layout weight example

Flexible Height in Vertical Orientation

To make height of a child flexible, you must set it height to [cci]0dp[/cci] and layout_weight greater than 0.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="@string/button2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button3" />

         />
</LinearLayout>

Here is the result:

Layout weight example on vertical orientation
Layout weight example on vertical orientation

Setting margin to child

Linear Layout respects to margin attribute on child. To set margin, using [cci]android:layout_margin[/cci]. This attribute will apply to all 4 sides, top, right,bottom and left. If you need just apply margin with specified side, you can use [cci]android:layout_marginLeft[/cci],[cci]android:layout_marginTop[/cci],[cci]android:layout_marginRight[/cci],[cci]android:layout_marginBottom[/cci]. Note, you must put margin value in [cci]dp[/cci] unit.
Here is an example:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="@string/button2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button3" />

         />
</LinearLayout>

Here is the result:

Layout margin example
Layout margin example

Setting gravity to child

Another feature of Linear Layout is gravity of a child. Gravity is an aligment of child with its parent. I will give tutorial about gravity in the next tutorial.