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?

CrazyLearner
  • 782
  • 3
  • 11
  • 28
  • use [ExpandableList](http://androidexample.com/Custom_Expandable_ListView_Tutorial_-_Android_Example/index.php?view=article_discription&aid=107&aaid=129) instead of ListView to create list with subitems – ρяσѕρєя K Dec 02 '13 at 04:27
  • add another textview below First text view in your RowLayout.xml – Jigar Pandya Dec 02 '13 at 04:48
  • @CrazyLearner Its better you should go with the `SectionListView` if you want to add the sub items for each items. – GrIsHu Dec 02 '13 at 04:59
  • @JigarPandya , Yes I have done that, but How can I add that in the adapter? – CrazyLearner Dec 02 '13 at 05:00
  • @CrazyLearner: http://androidexample.com/How_To_Create_A_Custom_Listview_-_Android_Example/index.php?view=article_discription&aid=67&aaid=92 -- It is an example according to your requirement.Hope it helps you – Jigar Pandya Dec 02 '13 at 05:03
  • @CrazyLearner Check out my answer which will surely help you. – GrIsHu Dec 02 '13 at 05:06

4 Answers4

30

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

enter image description here

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Thein
  • 3,940
  • 2
  • 30
  • 34
7

A ListView item can have it's own custom layout. When you create your adapter for the ListView you can pass in the layout id to the Adapter constructor. See SimpleAdapter and ArrayAdapter.

If you want to show some more details like image and text or two textview then You will have to extend an Adapter and implement getView() to property set the image+text.

Check out Custom ListView

And if you want to categorize the ListView in sections then you should go for the Section ListView in Android also check Section Header in ListView

GrIsHu
  • 29,068
  • 10
  • 64
  • 102
0

For that you have to create custom list view. Here is the Link...

Looking Forward
  • 3,579
  • 8
  • 45
  • 65
0

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 :)

Jigar Pandya
  • 2,129
  • 2
  • 17
  • 27