Handling Screen Orientation Android
It is observed that when device configurations including screen orientation, keyboard availability etc change, Android restarts the current Activity. This is done to enable the application to adapt to the new configuration.
In order to declare an Activity that handles configuration changes we must add the android:configChanges attribute with a value that represents the configuration we want to handle, in our AndroidManifest.xml file. There are basically two ways to handle screen orientation in Android. They are as follows!
From the Android developer’s page,
1. Retain an object during a configuration change
Allow your activity to restart when a configuration changes, but carry a stateful Object to the new instance of your activity.
2. Handle the configuration change yourself
Prevent the system from restarting your activity during certain configuration changes, but receive a callback when the configurations do change, so that you can manually update your activity as necessary.
Now, we will see how to handle screen orientation changes through Java code. Even if you don't have an Android device you can still check landscape and portrait mode for your application by using shortcut key combination of Ctrl + F12 for changing landscape to portrait mode in emulator.
Pre-requisites: Eclipse IDE (preferably latest version), Android SDK (tested it on Android 2.3.3, API level 10)
Programmatically set screen orientation in Android
//set screen orientation of activity (landscape)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//set screen orientation of activity (portrait)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Get the current screen orientation
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE getDefaultDisplay();
int orientation = display.getOrientation();
switch(orientation) {
case Configuration.ORIENTATION_PORTRAIT:
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case Configuration.ORIENTATION_LANDSCAPE:
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
}
Now, create a new Java class in any of your existing Android projects named OrientationActivity and add the following code!
OrientationActivity.java
import android.os.Bundle;
import android.app.Activity;
import android.content.res.Configuration;
import android.widget.Toast;
public class OrientationActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.orient_main);
}
//detect orientation change
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
//Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
Toast.makeText(this, "In landscape mode", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
{
Toast.makeText(this, "In portrait mode", Toast.LENGTH_SHORT).show();
}
}
}
orient_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Test button"/>
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="10" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.OrientationActivity"
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>
</application>
</manifest>
Run the project. Make sure no errors are present. You should see the following output on orientation change!
Note: Beginning with Android 3.2 (API level 13), the screen size also changes when the device switches between portrait and landscape orientation.
If you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the screenSize value in addition to the orientation value.