I need to be able to rotate whole layouts on the fly (on the click of a button).
I am able to rotate the layouts using, eg. layout.setRotation(270.0f)
. The problem is, after the rotation, the layout height
and width
are not matching its parent's.
I have tried inverting height
and width
like so,
RelativeLayout layout = (RelativeLayout)findViewById(R.id.rootLayout); LayoutParams layoutParams = layout.getLayoutParams(); int height = layout.getHeight(); int width = layout.getWidth(); layoutParams.height = width; layoutParams.width = height;
Which does nothing at all.
I am working with sdk 14
.
The first image below is the app as it starts. The second one, after a rotation. I wish to fill the black "space". Any help would be appreciated.
The images below show only a button in the layout. However, in reality, the layout are a lot more complex. What I am trying to achieve is "faking" a landscape view.
Edit: Changed images and added descriptions.
1 Swipe down the screen to access your Quick Settings and tap on Auto Rotate, Portrait or Landscape to change your screen rotation settings.
Not sure why this is useful, but it's a nice puzzle. Here is something that works for me:
On rotate click, do this:
RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.main); int w = mainLayout.getWidth(); int h = mainLayout.getHeight(); mainLayout.setRotation(270.0f); mainLayout.setTranslationX((w - h) / 2); mainLayout.setTranslationY((h - w) / 2); ViewGroup.LayoutParams lp = (ViewGroup.LayoutParams) mainLayout.getLayoutParams(); lp.height = w; lp.width = h; mainLayout.requestLayout();
And the layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:id="@+id/main" android:layout_height="match_parent" android:background="#ffcc88" tools:context=".TestRotateActivity" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Test" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" /> <Button android:id="@+id/rotate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Rotate" android:layout_centerInParent="true" /> </RelativeLayout>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With