In some cases, developers may want certain pages (activities) in their Android app to always display in landscape mode. This is useful for applications such as video players, games, and presentation-based apps where a wider screen enhances the user experience. Android provides a simple way to enforce this behavior using the android:screenOrientation attribute in the AndroidManifest.xml file.


Setting Landscape Orientation for Specific Activities

To force a specific activity to open in landscape mode, you need to modify its declaration inside the AndroidManifest.xml file by adding the attribute:

android:screenOrientation="landscape"

For example, consider the following AndroidManifest.xml snippet:


<activityandroid:name=".activity.MainActivity"
  android:screenOrientation="landscape"
  android:exported="true">
<activity
  android:name=".activity.ContactUs"
  android:exported="false" />
<activity
  android:name=".activity.AboutUs"
  android:screenOrientation="landscape"
  android:exported="false" />
<activity
  android:name=".activity.PrivacyPolicy"
  android:screenOrientation="landscape"
  android:exported="false" />

In this example:

  • MainActivity, AoutUs and Privacy Policy will always open in landscape mode.
  • ContactUs will follow the default orientation set by the system or app.

 

Applying Landscape Mode Globally

If you want all activities in your app to be displayed in landscape mode, instead of setting it individually for each activity, you can apply the setting globally in the <application> tag:


<application
 android:theme="@style/AppTheme"
 android:screenOrientation="landscape">
</application>

This will enforce landscape mode across all activities unless an individual activity explicitly overrides it.

 

By adding android:screenOrientation="landscape" to the AndroidManifest.xml file, you can force specific activities (or the entire app) to open in landscape mode. This is useful for applications that require a wider screen layout. However, always consider user preferences and device compatibility when enforcing orientation settings.