Search code examples
androidscreenorientationlandscapeportrait

Could I force switching portrait/landscape view on emulator?


Could I force changing the view's orientation in order to test how the layout appears or it is not possible to test this behavior on virtual device.

Under forcing it, I mean for example on click event to change from one to another screen orientation and inverse.


Solution

  • Ctrl+F11 will switch the screen orientation, but when it come to coding the orientation to change you have a couple of options:

    • Hardcode the Activity to be a given orientation. Which is useful when you want to force the user to use a specific orientation only.
    • Set the Activity to allow either orientation and let the user decide.

    Each of these is useful in their own scenarios. For example, viewing a graph that will only be useful in landscape should be hardcoded, but scrolling through a list could allow a user to pick which was more comfortable for them.

    How to set the mode:

    • In Eclipse go to the AndroidManifest->Application Tab, Select the Activity you want to edit and set the Screen Orientation directly.
    • In AndroidManifest.xml: <activity android:screenOrientation="[fill in preference here]" />
    • In code: Activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_[type])
    • To get the current orientation in code:
      Activity.getRequestedOrientation()

    An explanation of the different modes can be found here: Screen Orientation Types | Activity

    So, if you want to force a change on a screen based on user input, you can all you need to do is set the orientation type in code.