title: How to rotate views on orientation change without recreating layout?
body: <p>This question has been asked before <a href="https://stackoverflow.com/questions/10913248/rotating-views-but-not-layouts-on-screen-orientation-change">here</a> but its answer is incorrect. I would like to rotate some views on screen orientation change, but I want to keep the layout unchanged. They should be rotated 90, 180, 270 or 360 degrees according to the current orientation (<code>SCREEN_ORIENTATION_LANDSCAPE</code>, <code>SCREEN_ORIENTATION_PORTRAIT</code>, <code>SCREEN_ORIENTATION_REVERSE_LANDSCAPE</code>, <code>SCREEN_ORIENTATION_REVERSE_PORTRAIT</code>).</p>
  
  <p>This is what I want to achieve:</p>
  
  <p><a href="https://i.stack.imgur.com/FNWEl.png" rel="noreferrer"><img src="https://i.stack.imgur.com/FNWEl.png" alt="LayoutExample"></a> </p>
  
  <p>The answer in the link I mentioned stated that I should create a new different layout in <code>layout-land</code>. Clearly, this is not what I want. I don't want to recreate the activity or change layout orientation. I only want to rotate some views, and keep other views unchanged on orientation change.</p>
  
  <p><strong>There is a huge difference between <em>rotating</em> specific views and <em>changing or recreating</em> the whole layout (both on orientation change).</strong></p>
  
  <p>Using the answer in this <a href="https://stackoverflow.com/questions/10380989/how-do-i-get-the-current-orientation-activityinfo-screen-orientation-of-an-a">link</a>, I will be able to get the current screen orientation with this method:</p>
  
  <pre><code>public static int getScreenOrientation(Context context) {
      WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
      int rotation = windowManager.getDefaultDisplay().getRotation();
      DisplayMetrics dm = new DisplayMetrics();
      windowManager.getDefaultDisplay().getMetrics(dm);
      int width = dm.widthPixels;
      int height = dm.heightPixels;
      int orientation;
      // if the device's natural orientation is portrait:
      if ((rotation == Surface.ROTATION_0
              || rotation == Surface.ROTATION_180) &amp;&amp; height &gt; width ||
              (rotation == Surface.ROTATION_90
                      || rotation == Surface.ROTATION_270) &amp;&amp; width &gt; height) {
          switch (rotation) {
              case Surface.ROTATION_0:
                  orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                  break;
              case Surface.ROTATION_90:
                  orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                  break;
              case Surface.ROTATION_180:
                  orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
                  break;
              case Surface.ROTATION_270:
                  orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
                  break;
              default:
                  Log.e("ScreenOrientation", "Unknown screen orientation. Defaulting to " + "portrait.");
                  orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                  break;
          }
      }
      // if the device's natural orientation is landscape or if the device
      // is square:
      else {
          switch (rotation) {
              case Surface.ROTATION_0:
                  orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                  break;
              case Surface.ROTATION_90:
                  orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                  break;
              case Surface.ROTATION_180:
                  orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
                  break;
              case Surface.ROTATION_270:
                  orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
                  break;
              default:
                  Log.e("ScreenOrientation", "Unknown screen orientation. Defaulting to " + "landscape.");
                  orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                  break;
          }
      }
  
      return orientation;
  }
  </code></pre>
  
  <p>On orientation change, I would like to do something simple like this:</p>
  
  <pre><code>RotateAnimation rotateAnimation = new RotateAnimation(0, getScreenOrientation(getContext()));
  rotateAnimation.setDuration(2000);
  
  for (int i = 0; i &lt; 63; i++) {
      Button button = (Button) rootView.findViewById(i);
      button.startAnimation(rotateAnimation);
  }
  </code></pre>
  
  <p>Another way to rephrase my question would be "Is there any way to detect orientation change in <code>onConfigurationChanged()</code> method without changing the layout?". The problem is that it will not be able to detect any orientation change if I already disable layout orientation change.</p>
  
  <p>Anyone knows how it is done? I might have totally gone through wrong steps, and I think I will have to use Accelerometer Sensor or something similar to that to achieve what I want, so please guide me through.</p>