Reputation: 113
I have a RecyclerView and a LinearLayoutManager. Each line consists of a CheckBox and two TextViews.
How can I set the width of each TextView to the maximum width of its "column" so that the columns are left-aligned?
In the screenshot, I want the b's in the second "column" left-aligned.
I've tried the following:
public void test(View view)
{
int max = 10;
for (int i = 0; i < recyclerView.getLayoutManager().getChildCount(); i++)
{
View v = recyclerView.getLayoutManager().findViewByPosition(i);
TextView tv1 = v.findViewById(R.id.textView1);
if (tv1.getWidth() > max)
max = tv1.getWidth();
}
for (int i = 0; i < recyclerView.getLayoutManager().getChildCount(); i++)
{
View v = recyclerView.getLayoutManager().findViewByPosition(i);
TextView tv1 = v.findViewById(R.id.textView1);
tv1.setWidth(max);
}
This does not work and has the effect that the width of "axxx" get's smaller so that the letters are painted one below the other. The first loop correctly calculates the maximum width but after asigning the width in the second loop the text somehow collapses (because of WRAP_CONTENT?).
Edit: xml-file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_weight="1"
android:text="CheckBox" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_weight="1"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
I want to have the items look like in a table.
Upvotes: 0
Views: 634
Reputation: 1706
When you use textView.getWidth()
it will usually return 0 value, that is why your max never changes. To get the real width of the TextView
you need to use getMeasuredWidth()
like this:
textView1.measure(0,0);
if (textView1.getMeasuredWidth() > max) {
max = textView1.getMeasuredWidth();
}
textView2.setWidth(max);
And with that you get this:
But be careful. If your first TextView
width is too large it could push your second TextView
out of the layout. You need to check the device width and/or set maxWidth
to the desired value. I don't know how sure you can be that your data won't ever create this issue in the future. Happy coding!
Upvotes: 1