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: 
Simple and tricky way to make screen orientation along the button click.. with an example..

Here,I'm using the sharedPreference(Im setting an boolean value based on orientation ) 

Method for button onClick.

```
public void rotate(View v) {
    edt = prefs.edit();

    if (!prefs.getBoolean("screen_protrait", true)) {
        edt.putBoolean("screen_protrait", true);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    } else {
        edt.putBoolean("screen_protrait", false);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }
    edt.commit();
}

```

In xml, set an onClick method for rotate button

```
       <Button
   android:id="@+id/bt_rotate"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_centerInParent="true"
   android:onClick="rotate"
   android:text="Rotate" />

```

Last one, is in onCreate of Activity you want to set the Prefernce from Application..as

```
  prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

```

Keep coding.. You can achieve your goal...Let me know,if it's working with your scenario.
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>

```