How to update match_parent in linearLayout after using setRotation?

I have several nested layouts that I'm trying to rotate 90 degrees on demand in code. I have a setRotation function that works fine, but unfortunately with rotation it does not change completely correctly. The width on these elements is set to match_parent, and after rotation, it still matches the parent width, not the parent height that it should match.

XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/mainLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="link.basiclifecounter.LifeCounter" android:background="#CC00CC"> <LinearLayout android:id="@+id/topPlayers" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="2" android:orientation="vertical" android:background="#CC0000"> <RelativeLayout android:id="@+id/p3" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> **A bunch of stuff in here** </RelativeLayout> <RelativeLayout android:id="@+id/p2" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> **A bunch of stuff in here** </RelativeLayout> </LinearLayout> <LinearLayout android:id="@+id/bottomPlayer" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="vertical" android:background="#00CC00"> <RelativeLayout android:id="@+id/p1" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> **A bunch of stuff in here** </RelativeLayout> </LinearLayout> </LinearLayout> 

Spinning java code

 view.findViewById(R.id.topPlayers).setRotation(90); //Rotate the entire top box view.findViewById(R.id.p3).setRotation(180); //Flip one side so both players face outwards 

This picture shows the image before the start of rotation.

This image shows the image after rotation.

As you can see, the whole box was rotated after the height and width were already set. In the non-rotating version, the width (match_parent) should be full, and the height (layout_weight = 2) should be 2 / 3rds of the screen. This works great. The problem is that after its rotation, these dimensions remain unchanged, and do not adapt and change to a new rotation.

I chose bright background colors to help troubleshoot that the Pink you see is in the main LinearLayout, and not in any of the layouts that rotate.

I tried to enable rotation in the XML itself, and not inside the code, and got the same results as after the picture, so I clearly understand that I don’t understand how to get the width of the layout as I want. Can I just not use layout_weight efficiently with rotation?

+7
android android-layout rotation android-xml android-layout-weight
source share
1 answer

I think you have the same problem as in this Stack Overflow question. It seems that rotation is simply not taken into account for layout purposes. In other words, the layout occurs, then the rotation occurs with the views set out before the rotation.

This answer to the same question may help you.

You can also manually adjust the measurements. It’s a little dirty anyway, but I think it is.


You can download an alternative layout that will behave the way you want, instead of making a turn. (See Layout and image below.) You can also achieve the same result by changing the layout settings programmatically.

Here is a GitHub project that demonstrates the rotation programmatically. The view rotates after a pause for three seconds.

I hope you find this helpful.

enter image description here

alternate.xml

  <LinearLayout android:id="@+id/topPlayers" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="2" android:background="#CC0000" android:orientation="horizontal"> <RelativeLayout android:id="@+id/p3" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:orientation="vertical" android:rotation="90"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="p3" android:textSize="48sp" /> </RelativeLayout> <RelativeLayout android:id="@+id/p2" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="#0000CC" android:gravity="center" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:rotation="-90" android:text="p2" android:textSize="48sp" /> </RelativeLayout> </LinearLayout> <LinearLayout android:id="@+id/bottomPlayer" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="#00CC00" android:orientation="vertical"> <RelativeLayout android:id="@+id/p1" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="p1" android:textSize="48sp" /> </RelativeLayout> </LinearLayout> 

+1
source share

All Articles