3

I have a ListView and I wanted to place a small rectangle on the side of each item. To do so I made the following rendered layout:

rendered 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="match_parent"
    android:padding="3dp">

    <View
        android:layout_width="5dp"
        android:layout_height="fill_parent"
        android:id="@+id/item_color_image"
        android:background="@color/wallet_holo_blue_light"
        android:layout_marginRight="3dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginTop="3dp"
        android:layout_marginBottom="3dp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/item_icon_image"
        android:src="@drawable/m"
        android:layout_toRightOf="@+id/item_color_image"
        android:layout_centerVertical="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Business Name"
        android:id="@+id/item_title_text"
        android:layout_toRightOf="@+id/item_icon_image"
        android:layout_marginLeft="3dp"
        android:textStyle="bold"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:layout_alignTop="@+id/item_icon_image" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Distance"
        android:id="@+id/item_distance_text"
        android:layout_alignLeft="@+id/item_title_text"
        android:layout_alignBottom="@+id/item_icon_image"
        android:layout_marginBottom="-10dp" />
</RelativeLayout>

When I inflate this layout in a frame, it works just fine.....BUT when I inflate it inside a ListView I get this:

Error in List

The rectangle's height is being ignored to match the container's height.

I inflate it using this code:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View itemView = convertView;

        if (itemView == null) {
            itemView = inflater.inflate(R.layout.business_item_view, parent, false);
        }

        Business currentBusiness = businesses.get(position);

        ImageView icon = (ImageView) itemView.findViewById(R.id.item_icon_image);
        icon.setImageBitmap(currentBusiness.getIcon());
        TextView name = (TextView) itemView.findViewById(R.id.item_title_text);
        name.setText(currentBusiness.getName());
        TextView distance = (TextView) itemView.findViewById(R.id.item_distance_text);
        distance.setText("2 Km");  // TODO: Calculate distance

        return itemView;
    }

How can I fix this? Thanks!!

SPatrickApps
  • 538
  • 8
  • 21
Michel Feinstein
  • 13,416
  • 16
  • 91
  • 173
  • I don't see something that required relative layout in item layout. Why not to convert it to linear layout? – Eugen Martynov Jul 11 '14 at 13:22
  • Because I have the `color bar` at the left of 2 items, that will be possible only with 2 `LinearLayout`s nested, and nesting layouts has some performance impacts....also I have some other stuff on the sides, which won't function in a `LinearLayout` as well – Michel Feinstein Jul 12 '14 at 23:13
  • But relative layout requires twice re-layout. So by performance it is not so clear what is better – Eugen Martynov Jul 13 '14 at 08:03
  • I don't know the specifics...I was just following the general rule in the documentations...my view got a little more complicated that that is shown in here, and it will require lots of linear layouts inside one another, but now that you pointed out about relative layuot I am not sure whats better, at least its cleaner for me to see whats going on – Michel Feinstein Jul 13 '14 at 23:57

1 Answers1

1

Having your root item for a ListView row be match_parent for height doesn't make sense; each item would fill the ListView. I expect that's automatically being changed to wrap_content somewhere.

And RelativeLayouts don't support children with match_parent for height if the RelativeLayout's height is wrap_content. It works with LinearLayouts, so you should look into switching to that. Source: combining wrap_content on parent and fill_parent on child

Community
  • 1
  • 1
Coeffect
  • 8,772
  • 2
  • 27
  • 42