19

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

user449689
  • 3,142
  • 4
  • 19
  • 37
  • Why `android:layout_height="0dp"` ? – Mariusz Jamro Feb 24 '12 at 23:22
  • 1
    What is actually happening? Are you seeing the items but with the wrong height? Are you seeing no items? There isn't anything obviously wrong with what you've posted, but seeing your adapter and knowing what you're seeing now would be helpful. – Sam Judd Feb 25 '12 at 04:03
  • @Secator the 0dp defines whether the weight will be applied to height or width. so in this example the weight of 1 will be applied to height as that is set to 0dp. – Dave Haigh Aug 12 '14 at 10:50

4 Answers4

67

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)
Community
  • 1
  • 1
Amol Brid
  • 956
  • 9
  • 5
  • Thanks Amol, I could not make sense of LayoutInflater, now it's all clear. I could make it work and most of all, I understood why it's working like that. Thanks a lot – user449689 Feb 26 '12 at 10:09
  • 1
    if you get unsupported operation: check out this: LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(mDisplayWidth / 3, 30); convertView.setLayoutParams(params); – cV2 Mar 16 '12 at 13:30
  • 11
    @Amol Brid, i get error `java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView ` – Alan Lai May 08 '12 at 10:52
  • 17
    to avoid @AlanLai's issue, add the parameter `false` to the method `inflate` – mdelolmo Dec 11 '12 at 09:04
  • 7
    @Alan Lai like this: `View v = inflater.inflate(R.layout.filtered_trip_row, parent,false);` – VSB Aug 16 '13 at 12:37
  • @Amol Brid i got too error java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView – Spry Techies Aug 28 '14 at 05:02
  • Thank you for mentioning "false" parameter of inflater. This little change solved my problem! – Alexey Jul 28 '16 at 04:35
9
@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

Spry Techies
  • 896
  • 6
  • 21
0
<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;
}
Tony
  • 3,605
  • 14
  • 52
  • 84
vrbsm
  • 1,188
  • 15
  • 22
0
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;
}
Ahamadullah Saikat
  • 4,437
  • 42
  • 39