0

I have a custom ListView and adapter, and this is the code for my listview_item.xml:

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

    <ImageView
        android:id="@+id/background"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="17.0dip"
        android:fontFamily="sans-serif-condensed"
        android:paddingTop="17.0dip"
        android:textColor="@color/white"
        android:textSize="24.0dip" />

    <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/title"
        android:layout_marginLeft="18.0dip"
        android:layout_marginTop="-3.0dip"
        android:fontFamily="sans-serif-condensed"
        android:textColor="#ffaaaaaa"
        android:textSize="13.0dip" />

    <Button
        android:id="@+id/btnRanks"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:text="Ranks" >
    </Button>

</RelativeLayout>

I want to set an image as the background of each list item, but when I call imageView.setImageDrawable(drawable);, I get a listview that looks like this: . You can see that the image fills the entire width of the listview, but not the height. How do I get the image to fill the listview's height as well?

By the way, this is the intended background image:

The Heist
  • 1,444
  • 1
  • 16
  • 32
Ankit Goyal
  • 437
  • 1
  • 7
  • 24

1 Answers1

3

In your ImageView, instead of

android:layout_height="match_parent"

use

android:layout_height="wrap_content"

I'd also remove the attribute

android:scaleType="centerCrop"
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • Hmmm the `listview` looks the exact same after that change. Any other suggestions? – Ankit Goyal Mar 18 '14 at 18:07
  • If the image is the same for all items... set it once in the xml layout rather than setting it by code. – Phantômaxx Mar 18 '14 at 18:08
  • Unfortunately, the image is different for each list item. That's why I'm changing it programmatically – Ankit Goyal Mar 18 '14 at 18:09
  • OK, so it makes sense. Just as an experiment (not to do in practice): what happens if you force the height to be as tall as the image in px? – Phantômaxx Mar 18 '14 at 18:11
  • Yeah but the image is still cropped/condensed. Do you have any other potential solutions? Thanks in advance!! – Ankit Goyal Mar 18 '14 at 18:14
  • What If you remove the scaleType attribute from the ImageView? It does not appear to be helpful in this context – Phantômaxx Mar 18 '14 at 18:17
  • Now the image does not fill the `listview's` width as well. [Here is a screenshot](http://i.imgur.com/qS9WOgw.png) – Ankit Goyal Mar 18 '14 at 18:23
  • How big is the image in relation to the device? What is its resolution in dpi? Did you use the right drawable folder? All things that matter. – Phantômaxx Mar 18 '14 at 18:26
  • I am lazy-loading [this image](http://i.imgur.com/GG7cLZZ.jpg) using a drawable manager I found online. [Here is the link to the DrawableManager](http://stackoverflow.com/a/559781/1467730). – Ankit Goyal Mar 18 '14 at 18:31
  • 72 dpi... 550*137. It should go in the xhdpi folder, but it should be brought to 320 dpi, if you want it to scale well on other devices. Now, a quick experiment before I go for dinner: try setting it in code by using imageView.setBackground(drawable); // set it as background, so it's automatically stretched – Phantômaxx Mar 18 '14 at 18:36