12

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>

Eric Galluzzo
  • 3,191
  • 1
  • 20
  • 20
Foenix
  • 991
  • 3
  • 10
  • 23

2 Answers2

18

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);
nfirex
  • 1,523
  • 18
  • 24
  • 3
    @Foenix, It depends on which layout is the parent of the View. But ViewGroup.LayoutParams in all cases may be suitable. – nfirex Dec 04 '12 at 20:31
  • 2
    I would advise you to change the height of your final View view = super.getView(position, convertView, parent). But in first initialization his LayoutParams can be null, that' why you can doin like this: if (params == null) { view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT), mRowHeight) } else { params.height = mRowHeight; } – nfirex Dec 04 '12 at 20:37
  • that is much better because row can consist of several other views, thank you again! – Foenix Dec 08 '12 at 14:03
  • 1
    @Foenix, you need to change width or height (the updated question says about the width)? Can you add full code of itemF.xml and method getView(), please. – nfirex Dec 10 '12 at 07:01
  • It was all about row height, not width.I updated the post with xml, method getView was already full. Today I noticed that row height WAS changed but in a strange way - all rows in the screen stay the old height, but when I scroll down the list all rows beneath change their height right way. – Foenix Dec 10 '12 at 11:44
  • 1
    @Foenix, I think problem was in set LayoutParams. I update answer. – nfirex Dec 10 '12 at 11:46
  • I wish I could be just a little as smart as you. Thank you very much, it works like a miracle :) – Foenix Dec 10 '12 at 21:47
5

I did something like that :

@Override
public View getView(int position, View convertView,ViewGroup parent) {
        View view = super.getView(position, convertView, parent);

        TextView textView=(TextView) view.findViewById(android.R.id.text1);
        textView.setHeight(30);
        textView.setMinimumHeight(30);



        / * Couleur de votre choix * / 
        textView . SetTextColor ( Couleur . BLACK );



        retourner voir ; 
    }

You must put both fields textView.setHeight (30); textView.setMinimumHeight (30); or it won't work. For me it worked, & i had the same problem.

omnisius
  • 229
  • 5
  • 12