10

I have made a android listView taking the help from Vogella.com using following layout and ListActivity class.

RowLayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="22px"
        android:layout_height="22px"
        android:layout_marginLeft="4px"
        android:layout_marginRight="10px"
        android:layout_marginTop="4px"
        android:src="@drawable/icon" >
    </ImageView>

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="20px" >
    </TextView>

</LinearLayout> 

MyListActivity.java

package de.vogella.android.listactivity;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MyListActivity extends ListActivity {
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
        "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
        "Linux", "OS/2" };
    // use your own layout
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        R.layout.rowlayout, R.id.label, values);
    setListAdapter(adapter);
  }

  @Override
  protected void onListItemClick(ListView l, View v, int position, long id) {
    String item = (String) getListAdapter().getItem(position);
    Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
  }
}

I want to add a sub item below the textView and keep the full text portion in the center of each row. How can I do it?

7
  • use ExpandableList instead of ListView to create list with subitems Commented Dec 2, 2013 at 4:27
  • add another textview below First text view in your RowLayout.xml Commented Dec 2, 2013 at 4:48
  • @CrazyLearner Its better you should go with the SectionListView if you want to add the sub items for each items. Commented Dec 2, 2013 at 4:59
  • @JigarPandya , Yes I have done that, but How can I add that in the adapter? Commented Dec 2, 2013 at 5:00
  • @CrazyLearner: androidexample.com/… -- It is an example according to your requirement.Hope it helps you Commented Dec 2, 2013 at 5:03

2 Answers 2

30

You can use the built-in android.R.layout.simple_list_item_2 to create two line textView.

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

This should have been accepted instead as it solves the problem way more concisely.
-1

In order to add sub items to your items in List View, Use Expandable List View.

Have a look at this example.

You can ask if you have any further queries :)

2 Comments

I don't want to make expandable list view, What I want is to display two textViews at a time in a row
Link doesn't work for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.