Saturday, 24 September 2011

Locking the screen orientation in Android

Update: I just detected that there is some problem with the appCompat v7:19 in Android Studio. If this option is set, the app compat (ActionBar) won't be shown and there is a problem with the drawer. Use the follow option in the manifest file instead:

android:screenOrientation="portrait"

Sometimes the screen orientation could be a problem. If the layout hasn't been prepared for landscape, when the user switch the screen orientation, the buttons and other UI elements could appears in wrong place. To avoid this, the easiest way is lock the screen orientation.

To do it, one way is using the method

this.setRequestedOrientation(int requestedOrientation)

inside of the method onCreate(Bundle savedInstanceState) of the activity. This method accepts several options as parameter.

Landscape:
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE

Portrait:
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT

For more information:

No comments:

Post a Comment