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: 
```
    // get root layout from activity's XML
    LinearLayout mParentLayout = (LinearLayout) findViewById(R.id.activity_main);

    // get screen size from DisplayMetrics if you need to rotate before the screen is shown
    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

    int height = displayMetrics.heightPixels;
    int width = displayMetrics.widthPixels;

    // if you are rotating after the view was shown
    // acquire width and height from the layout's parent's LayoutParams

    // calculate offset to move the view into correct position
    int offset = (width - height) / 2;

    // rotate the layout
    FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(height, width);
    mParentLayout.setLayoutParams(lp);
    // 90° clockwise
    mParentLayout.setRotation(90.0f);
    mParentLayout.setTranslationX(offset);
    mParentLayout.setTranslationY(-offset);

```

It may look like the suggested answer, but this displays how to get dimensions from DisplayMetrics and the calculating the offset for the translation is a little different because that's the only way it worked properly.
i'll suggest you rotate only button rather than rotating the whole layout like 

```
btn_rotate.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) 
{
    rotation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate);
    rotation.setFillAfter(true);
    btn_rotate.startAnimation(rotation);
}
});

```

rotate.xml
----------

```
<set>
    <rotate
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:duration="0"
          android:fromDegrees="270"
          android:pivotX="50%"
          android:pivotY="50%"
          android:startOffset="0"
          android:toDegrees="270" />
</set>

```