Reputation: 31
I'm using this method to calculate the height of listview, but it's not really exactly as I want. Because, my listview have a TextView could be multiline when the content is so long. It's right when TextView is oneline, but when it has 2 lines, it's wrong height. Thank you!
Please see the error in this picture
code
public static boolean setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter != null) {
int numberOfItems = listAdapter.getCount();
// Get total height of all items.
int totalItemsHeight = 0;
for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
View item = listAdapter.getView(itemPos, null, listView);
item.measure(0, 0);
totalItemsHeight += item.getMeasuredHeight();
}
// Get total height of all item dividers.
int totalDividersHeight = listView.getDividerHeight() *
(numberOfItems - 1);
// Set list height.
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalItemsHeight + totalDividersHeight;
listView.setLayoutParams(params);
listView.requestLayout();
return true;
} else {
return false;
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/red"
android:orientation="vertical">
<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/border_margin"
android:text="name"
android:paddingLeft="200dp"
android:textColor="@color/header_lounge"
android:textSize="@dimen/text_normal"
android:textStyle="bold" />
</LinearLayout>
Upvotes: 1
Views: 1306
Reputation: 15333
All the methods that are available to calculate height of ListView
based on children fail when the TextView
becomes multiline.
I too faced the same problem and after a lot of tries, I found the solution of this problem.
The main concept is if you set the exact number of lines your
TextView
is having then the height will be calculated correctly i.e. you any how need to do the following,
textView.setLines(numberOfLines)
With this your problem will be solved.
Now the next question is how you can come to know the exact number of lines your TextView will be having dynamically.
I will say it totally depends on your scenario. In my case what I did was,
textView.setText(fullString);
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int screenWidth = display.getWidth(); // Get full screen width
int eightyPercent = (screenWidth * 80) / 100; // Calculate 80% of it
// as my adapter was having almost 80% of the whole screen width
float textWidth = textView.getPaint().measureText(fullString);
// this method will give you the total width required to display total String
int numberOfLines = ((int) textWidth/eightyPercent) + 1;
// calculate number of lines it might take
textView.setLines(numberOfLines);
Upvotes: 5