<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Android on despatches</title><link>https://icle.es/tags/android/</link><description>Recent content in Android on despatches</description><generator>Hugo</generator><language>en</language><lastBuildDate>Fri, 20 Jun 2025 09:25:00 +0100</lastBuildDate><atom:link href="https://icle.es/tags/android/index.xml" rel="self" type="application/rss+xml"/><item><title>Android - Multi-line Select List</title><link>https://icle.es/2010/04/19/android-multi-line-select-list/</link><pubDate>Mon, 19 Apr 2010 23:37:54 +0000</pubDate><guid>https://icle.es/2010/04/19/android-multi-line-select-list/</guid><description>&lt;p>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.&lt;/p>
&lt;p>Lets start with the data. For the sake of this demo, lets use a simple contact
list:&lt;/p>
```java
package uk.co.kraya.android.demos.MultiLineList.domain;

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) {
 this.mobile = mobile;
 }

}
```</description><content:encoded><![CDATA[<p>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.</p>
<p>Lets start with the data. For the sake of this demo, lets use a simple contact
list:</p>
```java
package uk.co.kraya.android.demos.MultiLineList.domain;

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) {
        this.mobile = mobile;
    }

}
```
<p>Some straightforward fields and getters/setters</p>
<p>Next, we need an Adapter. For this one, we will use
a <a href="http://developer.android.com/reference/android/widget/ArrayAdapter.html">ArrayAdapter</a>.
We will extend it so that we can override
the <a href="http://developer.android.com/reference/android/widget/Adapter.html#getView%28int,%20android.view.View,%20android.view.ViewGroup%29">getView method.</a></p>
```java
package uk.co.kraya.android.demos.MultiLineList.domain.adapters;

import java.util.List;

import uk.co.kraya.android.demos.MultiLineList.domain.Contact;
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;
    }

    @Override
    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)
                        getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        // 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;
    }

}
```
<p>The key bit here is the getView method. We pick up
the <a href="http://developer.android.com/reference/android/view/LayoutInflater.html">LayoutInflater</a>
which we can use to pick up the view that defines
the <a href="http://developer.android.com/reference/android/widget/TwoLineListItem.html">TwoLineListItem</a>
view. This allows us to use two different snippets of text as part of the list.
We then pick up the each of
the <a href="http://developer.android.com/reference/android/widget/TextView.html">TextView</a>
items and set the text against them. The formatting of these item are defined in
the xml file.</p>
<p>The TwoLineListItem class also defines a placeholder for the selectedIcon. Check
the documentation for more info.</p>
<p>The xml file for the layout goes as follows:</p>
```xml
<?xml version="1.0" encoding="utf-8"?>
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TextView android:id="@android:id/text1"
        android:layout_marginTop="1dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:textStyle="bold" />

    <TextView android:id="@android:id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@android:id/text1"
        android:layout_alignLeft="@android:id/text1"
        android:paddingBottom="4dip"
        android:includeFontPadding="false"
        android:textSize="15sp"
        android:textStyle="normal" />

</TwoLineListItem>
```
<p>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!</p>
<p>Finally, we have the activity. In fact, we will be using a
<a href="http://developer.android.com/reference/android/app/ListActivity.html">ListActivity</a>
as follows:</p>
```java
package uk.co.kraya.android.demos.MultiLineList;

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

import uk.co.kraya.android.demos.MultiLineList.domain.Contact;
import uk.co.kraya.android.demos.MultiLineList.domain.adapters.ContactArrayAdapter;
import android.app.ListActivity;
import android.os.Bundle;

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

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

    private List getContacts() {

        List contacts = new ArrayList();

        Contact c;

        c = new Contact();
        c.setFirstName("Shriram");
        c.setLastName("Shrikumar");
        c.setMobile("07777777777");

        contacts.add(c);

        c = new Contact();
        c.setFirstName("John");
        c.setLastName("Doe");
        c.setMobile("MOBILE.NUMBER");

        contacts.add(c);

        return contacts;

    }
}
```
<p>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.</p>
<p>Run it and you should see something like:</p>
<p>
  <img src="/assets/2010/04/multilineselectdemo.png" alt="">

</p>
<p>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.</p>
<p>I have included all the files that I created/modified but if you want the whole
project tarred up, just drop me a note ;-)</p>]]></content:encoded></item></channel></rss>