Set ListView row height in code

android, button, height, listview

Solution

I think you must chage getView() method of your `SimpleCursorAdapter`, like this:

final SimpleCursorAdapter adapter = new SimpleCursorAdapter (context, cursor) {
    @Override
    public View getView (int position, View convertView, ViewGroup parent) {
        final View view = super.getView(position, convertView, parent);
        final TextView text = (TextView) view.findViewById(R.id.tvRow);
        final LayoutParams params = text.getLayoutParams();

        if (params != null) {
                params.height = mRowHeight;
        }

        return view;
    }
}

When you click on your button, you must change `mRowHeight` and notify `ListView` about changes like `adapter.notifyDataSetChanged()`. For the first value of `mRowHeight` you can set this:

mRowHeight = LayoutParams.WRAP_CONTENT

UPD:

If method hasStableIds() of your `BaseAdapter` return `false` (it return `false` as default) you must apply little changes in your `getView()` (you must set `LayoutParams` to your `View` manually):

LayoutParams params = view.getLayoutParams();
if (params == null) { 
    params = new LayoutParams(LayoutParams.MATCH_PARENT, mRowHeight); 
} else {
    params.height = mRowHeight;
}

view.setLayoutParams(params);

Problem

I have an `Activity` containing a `ListView` based on `SimpleCursorAdapter` and a button. When I click the button, I want the row height to get bigger. I decided to use `TextView` height for this on button `OnClick`: ``` TextView tvRow = (TextView) findViewById(R.id.tvRow); tvRow.setHeight(20); ``` but it changes only the first row of the list. I can change the height in the `Adapter`'s `getView` method: ``` TextView tvRow = (TextView) view.findViewById(R.id.tvRow); tvRow.setHeight(20); ``` but that's not the functionality I need; I need this on button `OnClick`. How can I change `ListView` Row height by tapping a button? Maybe even a trick :) Thank you for your time. UPDATE: When I put this code in `SimpleCursorAdapter` everything works fine, but in method `getView` in `BaseAdapter`: ``` @Override public View getView(int position, View convertView, ViewGroup parent) { View view = convertView; if (view == null) { view = lInflater.inflate(R.layout.itemF, parent, false); } ... final LayoutParams params = view.getLayoutParams(); if (params == null) { view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, mRowHeight)); }else { params.height = mRowHeight; } return view; ``` the list at display just ignores this and its row width stays the same. What is wrong? UPDATE: ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/llRow" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <CheckBox android:id="@+id/cbBox" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:id="@+id/llRow1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:paddingTop="5dp" > <TextView android:id="@+id/tvName" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Наименование фирмы" android:textAllCaps="true" android:textSize="14dp" /> <TextView android:id="@+id/tvAddr" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Адрес фирмы" android:textSize="10dp" /> </LinearLayout> ```

Original source