If you are developing an Android application using Kotlin and facing the issue where your Kotlin app rotation is not working on emulator, you are not alone. This is a common issue faced by many Android developers when testing on the Android Studio emulator. In this article, we will cover the possible causes, step-by-step solutions, and provide full code examples to fix the problem. By the end of this guide, you will have a clear understanding of why rotation might fail and how to make it work properly in your Android project.
Why App Rotation Fails on Emulator
There are several reasons why rotation may not work in an emulator:
- Emulator settings disabled: Auto-rotate might be turned off in emulator quick settings.
- Manifest configuration: The
AndroidManifest.xml
file might lock the screen orientation. - Activity code issue: Orientation changes might be disabled in the activity lifecycle.
- Emulator bug: Sometimes, emulator builds have issues with rotation support.
Checking Emulator Auto-Rotate Settings
First, make sure your emulator allows rotation:
- Run your emulator in Android Studio.
- Open the Quick Settings panel (drag from the top or use the three-dot menu).
- Ensure Auto-rotate is enabled.
Once enabled, the emulator should respond to rotation commands. If it still does not work, check your app configuration.
Fixing Orientation in AndroidManifest.xml
Sometimes, your AndroidManifest.xml
explicitly locks the orientation. Look for the following line:
<activity android:name=".MainActivity" android:screenOrientation="portrait"> </activity>
The above configuration locks the activity to portrait mode. To allow rotation, remove or adjust the android:screenOrientation
attribute:
<activity android:name=".MainActivity" android:configChanges="orientation|screenSize"> </activity>
Now, your activity will handle rotation changes properly.
Handling Rotation in Kotlin Activity
If you want your Kotlin app rotation to reload the layout when rotating, you do not need extra code because Android does it automatically by recreating the activity. However, if you want to preserve data during rotation, you need to use onSaveInstanceState
and onRestoreInstanceState
.
Example: Saving Data During Rotation
class MainActivity : AppCompatActivity() { private var counter: Int = 0 private lateinit var counterTextView: TextView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) counterTextView = findViewById(R.id.counterText) // Restore state after rotation if (savedInstanceState != null) { counter = savedInstanceState.getInt("counter_value") counterTextView.text = counter.toString() } val button: Button = findViewById(R.id.incrementButton) button.setOnClickListener { counter++ counterTextView.text = counter.toString() } } // Save data before rotation override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) outState.putInt("counter_value", counter) } }
In this example, the counter value will not reset when rotating the emulator screen.
Testing App Rotation in Emulator
After applying the above changes, test your app:
- Start the emulator in Android Studio.
- Click the Rotate Left or Rotate Right buttons in the emulator toolbar.
- Verify that the UI rotates and that your data is preserved.
Common Problems and Solutions
Problem | Cause | Solution |
---|---|---|
App does not rotate | Locked orientation in manifest | Remove android:screenOrientation or set it to “unspecified” |
App crashes on rotation | Unmanaged activity lifecycle | Use onSaveInstanceState and restore state |
Emulator ignores rotation | Auto-rotate disabled in emulator | Enable rotation in quick settings |
Best Practices for App Rotation
- Always handle activity lifecycle properly.
- Use
ViewModel
orLiveData
from Android Architecture Components to persist data across configuration changes. - Avoid locking screen orientation unless your app specifically requires it (e.g., games, camera apps).
- Test on multiple emulator versions and real devices to confirm behavior.
Conclusion
When your Kotlin app rotation is not working on emulator, the issue usually comes down to emulator settings or AndroidManifest configuration. By checking auto-rotate settings, removing orientation locks, and properly managing lifecycle events, you can ensure your app supports both portrait and landscape modes. Following best practices like using ViewModel
will make your application more stable and user-friendly.
For more details on handling activity lifecycle and orientation changes, you can check the official Android documentation here: Android Activity Lifecycle.