AndyD273
AndyD273

Reputation: 7279

Easy way to add images to an Android ListView

I have a lisview that I want to add images to. The examples that I found early on showed using simple_list_item_1, but it doesn't seem to allow what I want.

If possible, I would also like to be able to change the color of items indepentantly of each other. So the text of One would be red, Two blue, etc.

main.xml:

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

    <TextView android:id="@+id/textHeader"
         android:textSize="24px"
         android:textColor="#006699"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content" 
         android:gravity="center_horizontal"
         />
    <ListView  
         android:id="@android:id/list"
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content"
         />
     <TextView android:id="@android:id/empty"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Empty set"
         />
<TextView
    android:id="@+id/status_text"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    />
</LinearLayout>

main.java

public class sGuide extends ListActivity {
    private String[] mBooks = new String[]{ "One", "Two", "Three" };


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

        ArrayAdapter<String> booksAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mBooks);

        this.setListAdapter(booksAdapter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //mChecker.onDestroy();
    }
}

Edit: here is my new row layout. I would like to make it so that tapping anywhere on the row selects it, instead of having to tap the image or label.

<?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="45dp"
        android:layout_height="45dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="4dp"
        android:src="@drawable/icon"
         >
    </ImageView>

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="11dp"
        android:layout_marginBottom="9dp"
        android:text="@+id/label"
        android:textSize="22dp" >
    </TextView>

</LinearLayout>

Edit 2: Changed the linear layout to fill parent instead of wrap text. Now it works.

Upvotes: 3

Views: 19147

Answers (2)

Senthil Mg
Senthil Mg

Reputation: 3313

In this case you need to use CustomAdapters, do the stuff you needed in getView() method, refer the link below for listview http://www.vogella.de/articles/AndroidListView/article.html

Upvotes: 2

Janusz
Janusz

Reputation: 189484

For this kind of behaviour you need to implement your own adapter. As you are using an Arrayadapter you should extend Arrayadapter and overwrite the getView method to change the view of the ListItem in the way you want it to. Have a look at this ListView Tutorial for more information on this topic.

Upvotes: 4

Related Questions