Reputation: 29
ListView
item is not getting wrap content height as I need to show only
I tried to set dynamic height to item but it's not working.
Please provide other solutions.
String in it's item. I am using ArrayAdapter
and here is the code of TextView
that I am using for inflating the item.
Thank you
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="none"
android:gravity="center_vertical"
android:paddingBottom="@dimen/dp2"
android:paddingTop="@dimen/dp2"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="@color/white" />
I am trying to set ListView's items height using below code -
for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
View item = arrayAdapter.getView(itemPos, null, listView);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
item.setLayoutParams(layoutParams);
item.measure(item.getMeasuredWidth(), item.getMeasuredHeight());
totalItemsHeight += item.getMeasuredHeight();
}
Upvotes: 0
Views: 918
Reputation: 1139
Put
android:layout_height="wrap_content"
on your TextView cell layout. Should change the TextView height dinamically based on their content.
Delete this two lines:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
item.setLayoutParams(layoutParams);
And do you have your for code onCreate() or onStart()? because if it's onCreate it's probably that getMeasuredHeigh() returns 0 always.
Upvotes: 1
Reputation: 113
Try to give some hardcoded value and check if it works instead of wrap content or just rebuild the project and try again with wrap_content
Upvotes: 0
Reputation: 1799
Try to change as below:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="none"
android:gravity="center_vertical"
android:paddingBottom="@dimen/dp2"
android:paddingTop="@dimen/dp2"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="@color/white" />
Upvotes: 0