Search code examples
androidlistviewbackgroundbitmaptile

Android: tile bitmap in ListView item


I am trying to add a sidebar image to some of the items in a ListView. The image should occupy the full height of the item and be repeated vertically if necessary. Stretching the image is not an option.

After trying a few things I ended up modifying the code found here. Unfortunately, it does not seem to work within a ListView and the image is not repeated:

enter image description here

Main layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       />
</RelativeLayout>

ListView item layout:

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

    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@drawable/background"
        android:orientation="vertical">
    <ImageView
        android:id="@+id/listitem_sidebar"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"       
        android:src="@drawable/sidebar"
        android:visibility="invisible"/>    
    </LinearLayout>
    <TextView 
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="400dp"
        android:text="Test"
        android:layout_alignParentRight="true"/>
</RelativeLayout>

Background drawable:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/sidebar"
    android:tileMode="repeat" />

Background image:

enter image description here

Setting the sidebar globally for the whole ListView is not an option as I am using different View types for the items in my production code.

Edit: formatting


Solution

  • Alight, finally got it to work just the way I wanted. I added a transparent View below the TextView to force the ListView to measure the item's height and do a layout pass. Then I used the invisible View as an anchor for my side bar.

    The only file that needed to be modified was listview_item.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    
        <TextView 
            android:id="@+id/textview"
            android:layout_width="wrap_content"
            android:layout_height="400dp"
            android:layout_alignParentRight="true"
            android:text="Test"/>
        <View
            android:id="@+id/dummy"
            android:layout_below="@id/textview"
            android:layout_width="match_parent"
            android:layout_height="0dp"/> 
    
            <LinearLayout 
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@drawable/background"
            android:layout_alignParentTop="true"
            android:layout_above="@id/dummy"
            android:orientation="vertical">
        <ImageView
            android:id="@+id/listitem_sidebar"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"               
            android:src="@drawable/sidebar"
            android:visibility="invisible"/>    
        </LinearLayout>
    </RelativeLayout>
    

    Here's a screenshot of the result:

    enter image description here

    I removed the code for my previous workaround as it was more of a hack than anything and didn't work as well as this.