I have multiple list views inside a scrollable view, so I used a function that computes the height of a list view and sets it so the list view is not scrollable anymore.
public static void setHeightListView(ListView listView) {
Adapter adapter = listView.getAdapter();
int UNBOUNDED = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
int height = listView.getDividerHeight() * (listView.getCount() - 1);
for (int i = 0; i < adapter.getCount(); ++i) {
View child = adapter.getView(i, null, listView);
child.measure(UNBOUNDED, UNBOUNDED);
height += child.getMeasuredHeight();
}
ViewGroup.LayoutParams listViewParams = listView.getLayoutParams();
listViewParams.height = height;
listView.requestLayout();
}
This function has worked fine so far, but it doesn't compute the correct height for this in getView() in my adapter:
convertView = inflater.inflate(R.layout.activity_frame_no_rounded_corners, null);
activity_frame_no_rounded_corners.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:orientation="horizontal"
android:paddingHorizontal="10dp"
android:focusable="false"
android:background="@drawable/textview_white">
<LinearLayout
android:layout_width="match_parent"
android:focusable="false"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageview_modify"
android:layout_width="30dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:focusable="false"
android:gravity="center_vertical"
android:src="@drawable/ic_baseline_remove_circle_24" />
<LinearLayout
android:layout_width="match_parent"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:focusable="false"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textview_key"
android:text="Key"
android:textSize="15dp"
android:padding="7dp"
android:textColor="#000000"
android:focusable="false"
android:layout_width="0dp"
android:layout_weight="0.4"
android:layout_height="wrap_content"
android:hint="Vlue"/>
<View
android:id="@+id/vertical_bar_devider"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#dcdcdc"/>
<EditText
android:id="@+id/edittext_value"
android:text="Value"
android:textSize="15dp"
android:padding="7dp"
android:textColor="#000000"
android:layout_width="0dp"
android:layout_weight="0.6"
android:layout_height="wrap_content"
android:hint="Value"
android:background="@drawable/textview_white"
android:drawableRight="@drawable/ic_baseline_arrow_forward_ios_24_gray"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
If I use a different xml then it computes the right height. Also, if I remove the weight from the views it also computes the right height (but I need the weight for alignment, I couldn't find a way around it).
You can see in the Screenshot the different list views that I have (one for the names, one for the dob, one for the grade, one for the email, one for the gmc no, one for the specialities).
For testing purposes, they do not have any margins, so they should be one right after the other.
As you can see, the height of the email list view is computed correctly, but my function fails for the rest of the lists. If I change "GMC Number" to "Email" then the function computes the right height for the gmc no list view as well (idk why, it's just an observation).
I guess my problem has something to do with the weight, but I have no idea how to fix it.
Thank you in advance!
from Getting measured height in Android Studio doesn't work properly
No comments:
Post a Comment