Question: 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.

![When starting](https://i.stack.imgur.com/z3x4r.png)
![After rotation](https://i.stack.imgur.com/iU5D2.png)

**Edit:** Changed images and added descriptions.

Answer: 
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>

```
Try this code:

```
btnRotate.setOnClickListener(new OnClickListener()
{
    @SuppressLint("NewApi")
    @Override
    public void onClick(View v) 
    {
        int orientation = getResources().getConfiguration().orientation;
        switch(orientation)
        {
            case Configuration.ORIENTATION_LANDSCAPE:
                llparent.setRotation(270.0f);
                RelativeLayout.LayoutParams layoutParams_LandsScape =
                    new RelativeLayout.LayoutParams(
                        rlRoot.getHeight(), rlRoot.getWidth()); 
                layoutParams_LandsScape.setMargins(
                    rlRoot.getTop(), rlRoot.getRight(),
                    rlRoot.getBottom(), rlRoot.getLeft());
                layoutParams_LandsScape.addRule(RelativeLayout.CENTER_IN_PARENT);
                llparent.setLayoutParams(layoutParams_LandsScape);  
                break;
            case Configuration.ORIENTATION_PORTRAIT:
                llparent.setRotation(270.0f);
                RelativeLayout.LayoutParams layoutParams_Portrait =
                    new RelativeLayout.LayoutParams(
                        rlRoot.getHeight(), rlRoot.getWidth()); 
                layoutParams_Portrait.setMargins(
                    0, 0, rlRoot.getBottom(), rlRoot.getLeft());    
                layoutParams_Portrait.addRule(RelativeLayout.CENTER_IN_PARENT);
                llparent.setLayoutParams(layoutParams_Portrait);
                break;
            }
        }
    });
}

```

And XML:

```
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".RotateAnim">

    <RelativeLayout
        android:id="@+id/rlroot"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#090">

        <LinearLayout
            android:id="@+id/llParent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#900"
            android:gravity="center"
            android:orientation="vertical">

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:text="Button"/>
        </LinearLayout>
    </RelativeLayout>
</RelativeLayout>

```