Detecting the Change in Screen Orientation in Android
Screen orientation in Android refers to the direction in which the device screen is displayed. The two common orientations are portrait mode, where the screen is vertical, and landscape mode, where the screen is horizontal. When a user rotates the device, the orientation of the screen changes and the activity may be recreated.
Android allows developers to detect this orientation change so that the application can respond accordingly. The system notifies the activity when the device configuration changes, including screen orientation. This can be handled by checking the current orientation using the configuration information provided by the Android system.
To detect the orientation of the device in an activity, the developer can use the Configuration class. This class provides information about the current device configuration, including whether the screen is in portrait or landscape mode. The program can then perform different actions depending on the orientation.
The following example code shows how to detect the orientation of the device.
import android.content.res.Configuration;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int orientation = getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(this, "Portrait Mode", Toast.LENGTH_SHORT).show();
}
else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "Landscape Mode", Toast.LENGTH_SHORT).show();
}
}
}
In this program, the application checks the current orientation using getResources().getConfiguration().orientation. If the device is in portrait mode, the program displays a message saying “Portrait Mode”. If the device is in landscape mode, it displays “Landscape Mode”.
Another way to detect orientation change is by overriding the onConfigurationChanged() method. This method is called whenever the configuration of the device changes, such as when the user rotates the screen. Inside this method, the program can check whether the new orientation is portrait or landscape and perform the required actions.
Example code:
import android.content.res.Configuration;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "Landscape Mode", Toast.LENGTH_SHORT).show();
}
else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(this, "Portrait Mode", Toast.LENGTH_SHORT).show();
}
}
}
To make this method work, the orientation change must also be declared in the AndroidManifest.xml file.
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize">
</activity>
This tells Android that the activity itself will handle the orientation change instead of restarting the activity.
Controlling the Orientation of an Activity
Sometimes developers may want to restrict the activity to only one orientation. For example, a game application may work best only in landscape mode, while a reading application may work better in portrait mode. Android allows developers to control the orientation of an activity using the AndroidManifest.xml file or programmatically in Java code.
One simple way to control orientation is by setting the screenOrientation attribute in the manifest file. This attribute forces the activity to remain in a specific orientation regardless of how the device is rotated.
Example code in AndroidManifest.xml:
<activity
android:name=".MainActivity"
android:screenOrientation="portrait">
</activity>
In this example, the activity will always stay in portrait mode, even if the user rotates the device.
If the developer wants the activity to always appear in landscape mode, the value can be changed to landscape.
<activity
android:name=".MainActivity"
android:screenOrientation="landscape">
</activity>
The orientation can also be controlled programmatically inside the activity using Java code. This method allows the developer to change the orientation while the application is running.
Example code:
import android.content.pm.ActivityInfo;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
This line of code forces the activity to switch to landscape mode. Similarly, portrait mode can be set using SCREEN_ORIENTATION_PORTRAIT.
In simple terms, detecting screen orientation helps the application know whether the device is in portrait or landscape mode, and controlling orientation allows the developer to fix or change the screen direction according to the needs of the application.
