Enable or Disable Screen Orientation with Toggle Button - Kotlin Version

In this tutorial, you will learn how to implement a feature in an Android app using Kotlin that enables or disables screen orientation for a specific activity. You'll create a user interface with a toggle button, allowing users to control whether the screen orientation should be fixed or allowed to rotate based on device orientation changes. The tutorial will guide you through handling orientation changes programmatically, modifying the activity's behavior dynamically based on the state of the toggle button. This implementation ensures flexibility in how users interact with screen orientation preferences within your Android application.
Submitted on July 07, 2024

To create an Android app that allows users to enable or disable screen orientation for a specific activity, you can follow these steps. In this example, we'll create a simple interface with a toggle button to control the orientation.

Step-by-Step Implementation

1. Define UI Layout (activity_main.xml):

Open ‘activity_main.xml’ (located in res/layout directory) and add a ToggleButton to enable/disable orientation.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">

    <ToggleButton
        android:id="@+id/toggleOrientation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enable Orientation"
        android:checked="true" />

</LinearLayout>

    

2. Implement Logic in MainActivity (MainActivity.kt):

Open ‘MainActivity.kt’ and implement the logic to enable or disable screen orientation based on the state of the ToggleButton.


package com.example.orientationcontrol

import android.content.pm.ActivityInfo
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.CompoundButton
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        toggleOrientation.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked) {
                // Enable orientation (default behavior)
                requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
            } else {
                // Disable orientation
                requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LOCKED
            }
        }
    }
}

    
  1. Explanation:

    • ToggleButton (toggleOrientation): This toggle button allows the user to enable or disable orientation changes.
    • setOnCheckedChangeListener: This method listens for changes in the toggle button's state.
    • requestedOrientation: This property is used to set the screen orientation behavior:
      • ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED: Allows the system to decide the orientation based on device sensors and user settings.
      • ActivityInfo.SCREEN_ORIENTATION_LOCKED: Locks the orientation to its current state, ignoring sensor and user settings.
  2. Manifest File (‘AndroidManifest.xml’):

    • By default, activities in AndroidManifest.xml are set to handle orientation changes (android:screenOrientation="unspecified"). You can leave it as is to allow the activity to inherit the orientation behavior set programmatically.
  3. Testing:

    • Run the app on an Android device or emulator.
    • Toggle the button to see the effect on the activity's orientation behavior.

Notes:

  • This example demonstrates how to control screen orientation programmatically for a specific activity. Ensure you handle orientation changes responsibly to provide a good user experience.
  • Make sure to handle edge cases such as configuration changes (like orientation changes) in your app to maintain consistent behavior.

By following these steps, you can create an Android app that enables or disables orientation changes for a specific activity based on user preference. Adjustments can be made based on your specific requirements or additional functionality needed in your application.