Android Screen Orientation Example
- The screenOrientation is the attribute of activity element. The orientation of android activity can be portrait, landscape, sensor, unspecified etc.
- You need to define it in the AndroidManifest.xml file. Syntax:
<activity android:name="package_name.Your_ActivityName" android:screenOrientation="orirntation_type"> </activity> <activity android:name=" example.com.screenorientation.MainActivity" android:screenOrientation="portrait"> </activity> <activity android:name=".SecondActivity" android:screenOrientation="landscape"> </activity>
The common values for screenOrientation attribute are as follows:
Value | Description |
unspecified | It is the default value. In such case, system chooses the orientation. |
portrait | taller not wider |
landscape | wider not taller |
sensor | orientation is determined by the device orientation sensor. |
AndroidManifest.xml File: AndroidManifest.xml
- In AndroidManifest.xml file add the screenOrientation attribute in activity and provides its orientation. In this example, we provide "portrait" orientation for MainActivity and "landscape" for SecondActivity.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="example.com.screenorientation"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name="example.com.screenorientation.MainActivity" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".SecondActivity" android:screenOrientation="landscape"> </activity> </application> </manifest>