How to programmatically change the height of a list element after the list has been populated
I finally found a solution to my problem and I would like to share it with the rest of the community.
I'm using an arrayAdapter to inflate my rowlayout following is it's code:
public class MySimpleArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
private final Bitmap[] images;
private final String[] names;
public MySimpleArrayAdapter(Context context, String[] values, Bitmap[] images, String [] names) {
super(context, R.layout.rowlayout, values);
this.context = context;
this.values = values;
this.images=images;
this.names=names;
}
View rowView;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//View rowView;
rowView = inflater.inflate(R.layout.rowlayout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
TextView title=(TextView) rowView.findViewById(R.id.title1);
textView.setText(values[position]);
imageView.setImageBitmap(images[position]);
title.setText(names[position]);
return rowView;
}
}
My main activity extends ListActivity where the items for the list are fetched from some json files in an asynctask. In the onPostExecute method of AsyncTask my list is created with the fetched items.
Following is the code for the onListItemClick method:
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
int hei=v.getLayoutParams().height;
System.out.println("Height "+hei);
if (hei==LayoutParams.WRAP_CONTENT) {
v.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,105));
v.requestLayout();
}
else {
v.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
v.requestLayout();
}
}
The trick is to call the requestLayout method so that your layout is refreshed.
Sergios
Updated on June 13, 2022Comments
-
Sergios almost 2 years
I'm working on a list of news items. My list is an
Activity
that extendsListActivity
. I've also created anArrayAdapter
that I use to inflate the elements of my list. TheArrayAdapter
uses a simple layout with anImageView
and twoTextView
s. Everything works but when I override theonListItemClick()
method of the and try to resize the list elements when I click on them nothing happens.protected void onListItemClick(ListView l, View v, int position, long id) { v.getLayoutParams().height=300; }
I've tried the above with
LayoutParams
as wellHere is the layout of each row that I use to populate my list:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="100dp" android:background="@color/white" > <ImageView android:id="@+id/icon" android:layout_width="80dp" android:layout_height="80dp" android:layout_marginLeft="4dp" android:layout_marginRight="10dp" android:layout_marginTop="4dp" android:src="@drawable/ic_launcher" > </ImageView> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/title1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@+id/label" android:textColor="@color/black" android:textColorHint="@color/black" android:textSize="10dp" android:textStyle="bold" > </TextView> <TextView android:id="@+id/label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@+id/label" android:textColor="@color/black" android:textColorHint="@color/black" android:textSize="10dp" > </TextView> </LinearLayout> </LinearLayout>
Has anyone faced the same issue? Can anyone cast some light for a beginner in Android?