0

I have a listview with a custom adapter (tile) design. Everything should work fine but the thing is that the til (listview item background) stretches too much despite me giving layout_height a fixed value. Here's the code:

This is the xml for the custom tile. Notice the 85dp fixed height. The background drawable should NOT stretch beyond this hight?

<?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="85dp"
    android:background="@drawable/tile_job" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="9dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="13dp"
        android:text="Title" />

    <TextView
        android:id="@+id/pickup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="38dp"
        android:layout_alignLeft="@+id/title"
        android:textSize="11dp"
        android:text="Pickup Address" />

    <TextView
        android:id="@+id/destination"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/pickup"
        android:layout_below="@+id/pickup"
        android:layout_marginTop="6dp"
        android:textSize="11dp"
        android:text="Destination Address" />

    <TextView
        android:id="@+id/time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/destination"
        android:layout_alignParentRight="true"
        android:layout_marginRight="40dp"
        android:text="Time"
        android:textSize="11dp" />

    <TextView
        android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/time"
        android:layout_below="@+id/time"
        android:text="Date"
        android:layout_marginTop="7dp"
        android:textSize="11dp" />

</RelativeLayout>

Now I'm using this in a simple listview:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/app_bg" >

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/strip_top" >

        <TextView
            android:id="@+id/strip_text"
            style="@style/TopStripText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="Locate" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="280dp"
        android:layout_height="440dp"
        android:layout_below="@+id/relativeLayout1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:background="@drawable/bg_booking" >

        <ListView
            android:id="@+id/activebookings"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true" >
        </ListView>
    </RelativeLayout>

What is causing the tile to overstretch vertically?

===========

EDIT Here is the requested code:

Inflation of the layout:

public class JobAdapter extends BaseAdapter
{
    private ArrayList<Job> bookingArrayList;
    private LayoutInflater mInflater;

    public JobAdapter(Context context, ArrayList<Job> results)
    {
        bookingArrayList = results;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount()
    {
        return bookingArrayList.size();
    }

    @Override
    public Object getItem(int arg0)
    {
        return bookingArrayList.get(arg0);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder;
        if (convertView == null)
        {
            convertView = mInflater.inflate(R.layout.tile_job, null);
            holder = new ViewHolder();
            holder.date = (TextView) convertView.findViewById(R.id.date);
            holder.time = (TextView) convertView.findViewById(R.id.time);
            holder.pickup = (TextView) convertView.findViewById(R.id.pickup);
            holder.destination = (TextView) convertView.findViewById(R.id.destination);
            holder.title = (TextView) convertView.findViewById(R.id.title);

            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.date.setText(bookingArrayList.get(position).getDate());
        holder.time.setText(bookingArrayList.get(position).getTime());
        holder.pickup.setText(bookingArrayList.get(position).getPickup());
        holder.destination.setText(bookingArrayList.get(position).getDestination());
        holder.title.setText(bookingArrayList.get(position).getTitle());

        return convertView;
    }

    static class ViewHolder
    {
        TextView date;
        TextView time;
        TextView pickup;
        TextView destination;
        TextView title;
    }
}
Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
Asim
  • 6,962
  • 8
  • 38
  • 61

1 Answers1

1

Your creating you row with following:

convertView = mInflater.inflate(R.layout.tile_job, null);

Now try this:

convertView = mInflater.inflate(R.layout.tile_job, parent, false);

If you don't attach the parent layout the layout parameters from xml will be ignored. You set false as you don't attach the view to the root right away (this happens automatically when the view is returned). You can read more about this issue here.

Community
  • 1
  • 1
Warpzit
  • 27,966
  • 19
  • 103
  • 155
  • Basically it uses the parent to measure the size of the view. Without the parent (ViewGroup) the layout wont turn out with the right dimensions. – Warpzit Mar 04 '13 at 12:58
  • @Luksprog you can see here for much more info: http://stackoverflow.com/a/5027921/969325 – Warpzit Mar 04 '13 at 13:00
  • It doesn't use the parent to measure the size, using the parent in the `inflate` method means the inflated view will have the proper `LayoutParams` with the values set in the xml otherwise when the ListView will add the inflated view it will generate some `LayoutParams` which will not have the desired values. I've asked you this because I posted it before you did and I expected that someone which posted an answer has a better explanation than `so it gets the right info so to speak`. – user Mar 04 '13 at 13:02
  • @Luksprog I'm sorry for the misunderstanding. I've edited the answer to better explain what is going on instead of using a vague term. Thanks for pointing it out. – Warpzit Mar 04 '13 at 14:30
  • Since I'm new to this stuff, I didn't quite understand what happened there, but it solved my problem, to an extent (it created a small but manageable new one). The items now seem to be ignoring the margins etc that I gave to the single custom tile. But I guess I'll figure something out. Thanks @Luksprog. – Asim Mar 04 '13 at 14:35
  • @Asim try with padding instead. – Warpzit Mar 04 '13 at 14:37