Amit
Amit

Reputation: 3990

android listview row takes up full height

I have a relativelayout view for use with a listview. For some reason, even though I have set layout_height to wrap_content, the layout is taking up all the space it can. The result is that every row in the list is as tall as the screen!

I am not sure what is wrong with the view. It used to work just fine.

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

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/pay" />

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/imageView1"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:singleLine="true"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>

</RelativeLayout>

Here is the getView function.

public View getView(final int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                try {
                    final JSONObject holder = getItem(position);
                    convertView = inflateView(R.layout.dumb);
                    convertView.setOnClickListener(new OnClickListener() {
                        public void onClick(View v) {
                            try {
                                Intent i = new Intent(context, Pending.class);
                                i.putExtra("id", holder.getString("_id"));
                                context.startActivity(i);
                            } catch(Throwable e){
                                e.printStackTrace();
                            }
                        }
                    });
                    convertView.setOnLongClickListener(new OnLongClickListener(){
                        public boolean onLongClick(View v){
                            try {
                                final CharSequence[] items = {"Pay", "Delete"};
                                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                                builder.setTitle(holder.getString("service"));
                                builder.setItems(items, new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int item) {

                                    }
                                }).create().show();
                            } catch(Throwable e){
                                e.printStackTrace();
                            }
                            return true;
                        }
                    });
                    TextView t = (TextView)convertView.findViewById(R.id.amount);
                    t.setText(holder.getString("currency") + holder.getString("amount"));
                    setImg((ImageView)convertView.findViewById(R.id.pic), holder.getJSONObject("to").getString("pic"));
                    t = (TextView) convertView.findViewById(R.id.time);
                    t.setText(DateUtils.getRelativeTimeSpanString(holder.getLong("time")*1000, System.currentTimeMillis(), 0, DateUtils.FORMAT_ABBREV_ALL));
                    t = (TextView) convertView.findViewById(R.id.service);
                    t.setText(holder.getString("service")); // url, co-ords, or shop

                } catch(Throwable e) {
                    e.printStackTrace();
                }
            }
            return convertView;
        }

Upvotes: 2

Views: 1675

Answers (3)

Oderik
Oderik

Reputation: 2260

Make sure the two images you use are not too large.

Upvotes: 1

aqs
aqs

Reputation: 5672

If you know how tall the row would take approximately(eg: 60dp), then instead of giving wrap_content give layout_height="60dp" in the root RelativeLayout. You can then use layout_centerVertical to correctly align the views.

If that is not acceptable try giving different background colors to different views as a way to debug and see which view is responsible for taking all the space.

Upvotes: 0

Alex Lockwood
Alex Lockwood

Reputation: 83303

Not sure if this is the issue, but when you create a custom ListActivity layout, you need to set the list's screen layout as well as the layout of an individual row. Ensure that the resource file you pass to setContentView() contains a ListView object with the id android:id/list and that you are passing the row layout (as you posted above) to the appropriate adapter.

Upvotes: 0

Related Questions