Dynamic height for ListView string in android

I see a weird behavior with my ListView, which does not seem to get around.

I have a ListView that is populated with some data. Each line contains an image and three text labels. Here is my XML for the string (I inflate it in the Adapter getView method):

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:paddingTop="0dip" android:paddingBottom="0dip" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:id="@+id/Icon" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="2" android:padding="2dp" android:scaleType="fitCenter" android:contentDescription="@string/app_icon"/> <TextView android:id="@+id/TxtName" android:scrollHorizontally="false" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textColor="@android:color/black" android:background="@color/list_bg1" android:layout_weight="2" android:padding="2dp"/> <TextView android:id="@+id/TxtPackage" android:scrollHorizontally="false" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="2" android:textColor="@android:color/black" android:background="@color/list_bg2" android:padding="2dp"/> <TextView android:id="@+id/TxtSource" android:scrollHorizontally="false" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="2" android:background="@color/list_bg3" android:textColor="@android:color/black" android:padding="2dp"/> </LinearLayout> 

Now everything that is included in the text fields can be of any length (from several characters to 80-100 characters), and I want the labels to simply wrap and the height of the line to expand to fit the height of the wrapped label.

However, the labels wrap ok, but the line height does not expand, so the bottom of the labels is trimmed. Here is my getView method:

 @Override public View getView(int position, View convertView, ViewGroup parent) { SetInfo app = (SetInfo)getItem(position); if(app == null) return null; LinearLayout row = (LinearLayout) (convertView == null ? LayoutInflater.from(context).inflate(R.layout.listitem, parent, false) : convertView); ((TextView)row.findViewById(R.id.TxtName)).setText(app.getName()); ((TextView)row.findViewById(R.id.TxtPackage)).setText(app.getPackage()); ((TextView)row.findViewById(R.id.TxtSource)).setText(app.getSource()); if(app.getIcon() != null) ((ImageView)row.findViewById(R.id.Icon)).setImageDrawable(app.getIcon()); return row; } 

How can I have lines of different heights automatically adjusted to fit all content?

+6
source share
2 answers

Try changing your TextViews to android: layout_height = "wrap_content".

+8
source

I had a problem with sam and @maebe's answer did not work for me. To make it work, I had to call requestLayout () in my TextView in the getView adapter.

So, in your case, you will need to call requestLayout () for each TextView:

 @Override public View getView(int position, View convertView, ViewGroup parent) { // Inflate your view etc. row.findViewById(R.id.TxtName).requestLayout(); row.findViewById(R.id.TxtPackage).requestLayout(); row.findViewById(R.id.TxtSource).requestLayout(); return row; } 

I tried all kinds of tricks, and this is the only thing that worked in my case.

+4
source

Source: https://habr.com/ru/post/909747/


All Articles