General
Android SDK: Using Custom View In XML Based Layout
March 23, 2010
0

This tutorial demonstrates how to use a custom view class (that is a class derived from View or its subclass) in an XML layout. This demonstration assumes basic familiarity with Android SDK and its XML layouting mechanism, the code is written in Android SDK 2.1.

Creating The Custom View Class

To begin with, let’s create a custom view class. For demonstration purpose, the one I am using is below:

package com.permadi.example;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

public class CustomView extends LinearLayout
{
 public CustomView(Context context, AttributeSet attrs)
 {
   super(context, attrs);

   LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   View view=layoutInflater.inflate(R.layout.example,this);
 }
}

This class loads the XML file named example.xml in the line:

   View view=layoutInflater.inflate(R.layout.example,this);

It is not necessary for the custom view class to be associated with an XML like above (this topic is explained here: https://permadi.com/blog/2010/03/android-sdk-loading-layout-and-associating-to-a-class/), you can use other techniques, such as constructing the view manually in the code.

example.xml looks like this:

<TableLayout android:id="@+id/TableLayout01"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   xmlns:android="http://schemas.android.com/apk/res/android">
  <TableRow android:id="@+id/TableRow01"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content">
     <TextView android:id="@+id/TextView01"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:textSize="40px"
         android:text="Questionaire">
     </TextView>
     <RadioButton android:id="@+id/RadioButton01"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Yes" android:textColor="#0f0"
         android:layout_gravity="right"
         android:layout_alignParentRight="false">
     </RadioButton>
     <RadioButton android:id="@+id/RadioButton02"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="No" android:textColor="#f00"
         android:layout_gravity="right" android:layout_alignParentLeft="false"
         android:layout_alignParentRight="true"></RadioButton>
   </TableRow>
</TableLayout>

And the layout generated by above xml is shown below:

Adding The Custom View Class To A Layout XML

To use the custom class in another xml based layout, you can refer to it by the fully qualified class name (fully qualified means: com.example.permadi.CustomView or whatever the package name plus the class name is for your custom class, and not just the class name). For example, let’s use com.example.permadi.CustomView on main.xml. (Note: In Eclipse, with the current version of SDK, you cannot drag and drop the custom view object into the editor, instead you need to enter the fully qualified class name manually into the xml file.)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/container"
  android:layout_height="fill_parent" android:orientation="vertical" android:layout_width="fill_parent">
    <com.example.permadi.CustomView android:layout_height="wrap_content" android:layout_width="fill_parent"/>
    <com.example.permadi.CustomView android:layout_height="wrap_content" android:layout_width="fill_parent"/>
    <com.example.permadi.CustomView android:layout_height="wrap_content" android:layout_width="fill_parent"/>
</LinearLayout>

You can see that I created 3 instances of the CustomView in the main.xml – of course you can add more as needed.

Important: You must specify the layout_width and layout_height if you are not using the TableLayout, otherwise an exception will be thrown and the application will crash. Once you set the layout_width and layout_height, you can even use Eclipse editor to edit and see a preview:

In a real application, you might want to assign an unique id for each so that you can easily retrieve the instance(s), but in this example, I will simply load the main.xml and use it with setContentView in an Activity class.

package com.example.permadi;

import android.app.Activity;
import android.os.Bundle;

public class MyActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

Here’s how the example looks in Nexus One:

I hope this tips is useful to help speeding up development time. The example project, created with Eclipse can be found here:
https://permadi.com/blog/2010/03/android-sdk-using-custom-view-in-xml-based-layout-download-example-project/