user449689
user449689

Reputation: 3154

Set ListView item height

this should be a very easy thing to do, but it looks like it's very tricky. In my code I have this ListView:

<ListView android:id="@+id/sums_list" android:layout_width="fill_parent"
        android:layout_height="0dp" android:layout_weight="1" android:dividerHeight="0dp"></ListView>

That is populated with an ArrayAdapter that uses this view:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:cdpb="http://schemas.android.com/apk/res/ale.android.brainfitness"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="55dp">
...
</LinearLayout>

Why isn't the item 55dp high?

Thanks everybody

Upvotes: 19

Views: 42188

Answers (4)

Ahamadullah Saikat
Ahamadullah Saikat

Reputation: 4644

convertView = inflater.inflate(R.layout.custom_row, parent, false);

if (CONDITION) {
    holder.wholeLayout.getLayoutParams().height = 1; // visibility Gone not working && 0 height crash app.
} else {
    holder.wholeLayout.getLayoutParams().height = RelativeLayout.LayoutParams.WRAP_CONTENT;
}

Upvotes: 0

vrbsm
vrbsm

Reputation: 1218

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:minHeight="60dp"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center"
    android:weightSum="1"
    android:background="@color/main_color">

    <ImageView
        android:id="@+id/img_left_menu"
        android:layout_weight="0.4"
        android:focusable="false"
        android:maxHeight="50dp"
        android:maxWidth="50dp"
        android:minHeight="50dp"
        android:minWidth="50dp"
        android:layout_width="0dp"
        android:scaleType="centerInside"
        android:adjustViewBounds="true"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/tx_left_menu"
        android:layout_width="0dp"
        android:textSize="18dp"
        android:layout_gravity="center_vertical"
        android:layout_weight="0.6"
        android:singleLine="true"
        android:layout_height="wrap_content"
        android:focusable="false"
        />
</LinearLayout>

in the adapter set the parent as described above

@Override
public View getView(int position, View convertView, ViewGroup parent) {
  ...
  View v = inflater.inflate(R.layout.filtered_trip_row, parent,false);
  …

  return v;
}

Upvotes: 0

Amol Brid
Amol Brid

Reputation: 966

How are you inflating the view?

If you are setting 'parent' parameter to null like below, then layout parameters are ignored.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
  ...
  View v = inflater.inflate(R.layout.filtered_trip_row, null);
  …

  return v;
}

Passing 'parent' to inflate should work as you expect.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
  ...
  View v = inflater.inflate(R.layout.filtered_trip_row, parent);
  …

  return v;
}

This explains it in detail: Why does LayoutInflater ignore the layout_width and layout_height layout parameters I've specified?

To avoid getting UnsupportedOperationException, you can add false as an input parameter to the inflator. Example:

inflater.inflate(R.layout.filtered_trip_row, parent, false)

Upvotes: 67

Spry Techies
Spry Techies

Reputation: 906

@Override
public View getView(int position, View convertView, ViewGroup parent) {
  ...
  View v = inflater.inflate(R.layout.filtered_trip_row, parent,false);
  …

  return v;
}

just pass boolean value to the method so that it add the yourrowlayout to the listview and it will not give error futher like error java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView

and add yourLayout which u wants to populate through Adapter

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:cdpb="http://schemas.android.com/apk/res/ale.android.brainfitness"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="55dp">
...
</LinearLayout>

it works for me and i think it may help you

Upvotes: 9

Related Questions