---
title: "How can you handle orientation changes in an Android app?"  
description: "How can you handle orientation changes in an Android app?"  
author: "Steilla Mitchel"  
published: 2023-06-27  
updated: 2023-07-02  
canonical: https://www.mindstick.com/forum/158885/how-can-you-handle-orientation-changes-in-an-android-app  
category: "android apps"  
tags: ["android", "android application"]  
reading_time: 3 minutes  

---

# How can you handle orientation changes in an Android app?

How can you [handle](https://www.mindstick.com/articles/311004/suede-skillet-handle-cover) [orientation](https://www.mindstick.com/interview/2604/what-is-orientation) changes in an [Android app](https://www.mindstick.com/articles/208957/a-complete-guide-for-android-app-development)?

## Replies

### Reply by Aryan Kumar

Sure. There are a few ways to handle orientation changes in an [Android](https://www.mindstick.com/articles/13046/10-reasons-why-i-prefer-android-vs-iphone-ios) app. Here are a few of the most common methods:

- **Use the** `onConfigurationChanged()` **method.** The `onConfigurationChanged()` method is called whenever the device's configuration changes, such as when the orientation changes. In this method, you can update your app's UI to reflect the new orientation.
- **Use the** `setRequestedOrientation()` **method.** The `setRequestedOrientation()` method allows you to specify the orientation that you want your app to be displayed in. For example, you can use this method to force your app to be displayed in portrait mode even if the device is rotated to landscape mode.
- **Use the** `Configuration` **class.** The `Configuration` class provides information about the device's current configuration, including its orientation. You can use this class to determine the current orientation of the device and update your app's UI accordingly.

Here is an example of how to use the `onConfigurationChanged()` method to handle orientation changes:

Code snippet

```plaintext
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Register a listener for configuration changes
        this.registerConfigurationChangedListener(new ConfigurationChangedListener() {
            @Override
            public void onConfigurationChanged(Configuration newConfig) {
                // Update the UI to reflect the new orientation
                // ...
            }
        });
    }

    private class ConfigurationChangedListener implements ConfigurationChangedListener {
        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            // Update the UI to reflect the new orientation
            // ...
        }
    }
}
```

In this example, the `onConfigurationChanged()` method is called whenever the device's configuration changes. In this method, the `ConfigurationChangedListener` class is used to update the UI to reflect the new orientation.

Here is an example of how to use the `setRequestedOrientation()` method to force your app to be displayed in portrait mode:

Code snippet

```plaintext
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Force the app to be displayed in portrait mode
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
}
```

In this example, the `setRequestedOrientation()` method is used to force the app to be displayed in portrait mode. This means that the app will not be displayed in landscape mode, even if the device is rotated to landscape mode.

Here is an example of how to use the `Configuration` class to determine the current orientation of the device:

Code snippet

```plaintext
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the current configuration
        Configuration configuration = getResources().getConfiguration();

        // Determine the current orientation
        int orientation = configuration.orientation;

        // Update the UI to reflect the current orientation
        // ...
    }
}
```

In this example, the `configuration` variable is used to store the current configuration of the device. The `orientation` variable is then used to determine the current orientation of the device. This information can then be used to update the UI of the app accordingly.


---

Original Source: https://www.mindstick.com/forum/158885/how-can-you-handle-orientation-changes-in-an-android-app

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
