Android – Handling Screen Orientation

Do you want your application to run only in Portrait mode, or maybe in Landscape only? Or maybe you want to force a change of orientation during execution despite of the real orientation of the device? Or maybe you want the orientation not to change when the mini-keyboard is opened? Here’s what you can do:

Force a specific screen orientation

-    In the AndroidManifest.xml of your application set android:screenOrientation attribute for each Activity you want with a fixed orientation to either “portrait” or “landscape”. See below an example:

<activity android:name=".Logo"
    android:screenOrientation="portrait"
    android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
  • If you want to make sure the orientation changes based on accelerometer set the above screenOrientation attribute to “sensor

or

-    In the code of your Activity you can use setRequestedOrientation() command like in the following snippet:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // use for landscape mode
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

Manage screen orientation in code

If you want to handle the recreation of views (or anything else actually) when your device’s orientation is changed or the mini-keyboard is inserted/pulled out you should do the following:

  • Set the android:configChanges attributes on the <activity> element in AndroidManifest.xml to “orientation|keyboardHidden“:
<activity android:name=".Logo"
    android:configChanges="orientation|keyboardHidden"
    android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
  • By doing that when a change in mini-keyboard status or orientation occurs, the onConfigurationChanged event will be fired and you can override it and set any behavior you want:
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // manage redrawing of your activity here
    // ...
}

By using ConfigChanges attribute you can access configuration changes that the Activity will normally handle by itself. If a change that you’ve selected in that attribute occurs, the onConfigurationChanged method is called, that’s why you can manually set any behavior you want in your application by overriding it. Take a look here for more on configChanges to see what other changes you can manage with this.

If you need to find at runtime the screen orientation or other screen properties take a look at the Screen Size, Orientation and Pixel Density Snippet.

orientation|keyboardHidden

Leave a Reply

You must be logged in to post a comment.