Android get fragment width

I have a layout with three fragments:

<LinearLayout android:id="@+id/acciones" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <LinearLayout android:id="@+id/fragment1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"> </LinearLayout> <LinearLayout android:id="@+id/fragment2" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"> </LinearLayout> <LinearLayout android:orientation="vertical" android:id="@+id/f3" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"> </LinearLayout> </LinearLayout> 

In the first snippet, I have a TableLayout in which I have one custom TextView in each row. I want to know the width of the fragment, because if the custom TextView is wider than the fragment, I set the required number of lines.

This is what I did in my custom TextView:

 @Override protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); mMaxWidth = (float) (getMeasuredWidth()); } 

In this line, I got the width of the three fragments, not just the one that contains the custom TextView.

Thanks.

+6
source share
1 answer

You should be able to set the width of the TextView for fill_parent, in which case it will do the wrapper for you. You should not set the width of your layouts as match_parent, as it is inefficient when you use layout layouts.

Since the Android layout system is sometimes mysterious regarding view sizes, if setting the TextView width to fill_parent actually makes it take up the whole screen (as your question seems to imply), follow these steps:

Set the width of the TextView to 0 by default. In onCreate your activity, after setting up the content:

 findViewById(R.id.acciones).getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { final int fragmentWidth = findViewById(R.id.releventFragmentId).getWidth(); if (fragmentWidth != 0){ findViewById(R.id.yourTextViewId).getLayoutParams().width = fragmentWidth; } } }); 

By initially setting the width of the TextView to 0, you do not allow it to change the width of the fragments. Then you can use the view tree observer to get the width of any fragment that interests you (by looking at its root view) after the layout has occurred. Finally, you can adjust the TextView to the exact width, which in turn will automatically wrap it.

Note that onGlobalLayout can be called several times and called regularly before all views are fully laid out, therefore, check! = 0. You also probably want to do some kind of check to make sure that you only set the width of the text view once, otherwise you may end up in an endless layout (but not at the end of the world, but not for performance).

+25
source

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


All Articles