Android – Multi-line Select List

It turns out that it is surprisingly easy to add a multi line select list to the UI.
There are four main parts to it. The layout file, a subclass to the adapter, the activity and of course the data itself.

Lets start with the data. For the sake of this demo, lets use a simple contact list:


public class Contact {

	private String firstName;

	private String lastName;

	private String mobile;

	public String getFirstName() {
		return firstName;

	public void setFirstName(String firstName) {
		this.firstName = firstName;

	public String getLastName() {
		return lastName;

	public void setLastName(String lastName) {
		this.lastName = lastName;

	public String getMobile() {
		return mobile;

	public void setMobile(String mobile) { = mobile;


Some straightforward fields and getters/setters

Next, we need an Adapter. For this one, we will use a ArrayAdapter. We will extend it so that we can override the getView method.


import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TwoLineListItem;

public class ContactArrayAdapter extends ArrayAdapter {

	private final int resourceId;

	public ContactArrayAdapter(Context context, int textViewResourceId, List objects) {
		super(context, textViewResourceId, objects);
		resourceId = textViewResourceId;

	public View getView(int position, View convertView, ViewGroup parent) {

		Contact c = getItem(position);

		// if the array item is null, nothing to display, just return null
		if (c == null) {
			return null;

		// We need the layoutinflater to pick up the view from xml
		LayoutInflater inflater = (LayoutInflater)

		// Pick up the TwoLineListItem defined in the xml file
		TwoLineListItem view;
		if (convertView == null) {
			view = (TwoLineListItem) inflater.inflate(resourceId, parent, false);
		} else {
			view = (TwoLineListItem) convertView;

		// Set value for the first text field
		if (view.getText1() != null) {
			view.getText1().setText(c.getFirstName() + " " + c.getLastName());

		// set value for the second text field
		if (view.getText2() != null) {
			view.getText2().setText("mobile: " + c.getMobile());

		return view;


The key bit here is the getView method. We pick up the LayoutInflater which we can use to pick up the view that defines the TwoLineListItem view. This allows us to use two different snippets of text as part of the list. We then pick up the each of the TextView items and set the text against them. The formatting of these item are defined in the xml file.

The TwoLineListItem class also defines a placeholder for the selectedIcon. Check the documentation for more info.

The xml file for the layout goes as follows:

<?xml version="1.0" encoding="utf-8"?>
<TwoLineListItem xmlns:android=""

    <TextView android:id="@android:id/text1"
        android:textStyle="bold" />

    <TextView android:id="@android:id/text2"
        android:textStyle="normal" />


As you can see, we are just defining a TwoLineListItem element with two embedded TextItems. Tthe android:id parts are important. It ensures that the getText1() and getText2() methods work as expected!

Finally, we have the activity. In fact, we will be using a ListActivity as follows:


import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;

public class MultiLineListDemo extends ListActivity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {

		setListAdapter(new ContactArrayAdapter(this, R.layout.main, getContacts()));

    private List getContacts() {

    	List contacts = new ArrayList();

    	Contact c;

    	c = new Contact();


    	c = new Contact();


    	return contacts;


getContacts clearly just creates a couple of contacts for the sake of the demo. its the setListAdapter that is the key here. It creates a new ContactArrayAdapter that we have written, passes in the context (which is just the current activity), a resource ID and a List of items to display.

Run it and you should see something like:

Final Output

Final Output

So easy when you know how. I believe could use a View or a ViewGroup as needed instead of the TwoLineListItem but I shall leave that to you to discover.

I have included all the files that I created/modified but if you want the whole project tarred up, just drop me a note 😉

One thought on “Android – Multi-line Select List

  1. Pingback: Activity und View -> Variablenzugriff -

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.