Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a sub item in Android ListView?

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?

like image 836
CrazyLearner Avatar asked Dec 02 '13 04:12

CrazyLearner


2 Answers

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

enter image description here

like image 129
Thein Avatar answered Nov 16 '22 06:11

Thein


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

like image 28
GrIsHu Avatar answered Nov 16 '22 05:11

GrIsHu