In some cases you want to fix the display orientation for your app in landscape or portrait. In this post i will explain how to restrict the orientation of your app only in landscape or portrait mode.
You can set the display orientation by using the following method
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
If you want to restrict the display orientation in portrait mode, then you can use the following.
MainActivity.java
You can set the display orientation by using the following method
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
If you want to restrict the display orientation in portrait mode, then you can use the following.
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Also you have to made some changes in the androidmanifest.xml file as follows.
<activity
android:name="com.orientationdemo.MainActivity"
android:label="@string/app_name"
android:screenOrientation="landscape" >
MainActivity.java
package com.orientationdemo;
import android.os.Bundle;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.view.Display;
import android.view.Menu;
import android.view.WindowManager;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.orientationdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.orientationdemo.MainActivity"
android:label="@string/app_name"
android:screenOrientation="landscape"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>