How to Listen to Orientation Change in Android?
In Android, applications can have orientations of types namely portrait and landscape. By default, every new project when created comes with a portrait orientation. However, this can be changed to landscape or semi. In the case of the semi, both portrait and landscape orientation is supported by the application depending upon the standing orientation of the device. Also, it is possible to detect when the orientation changes from one to the other.
So in this article, we will show you how you could listen to orientation changes in Android. Follow the below steps once the IDE is ready.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Working on AndroidManifest.xml
In the activity tag, set configChanges to orientation|screenSize to implement semi orientation.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < manifest xmlns:android = "http://schemas.android.com/apk/res/android" package = "org.w3wiki.orientationchange" > < application android:allowBackup = "true" android:icon = "@mipmap/ic_launcher" android:label = "@string/app_name" android:roundIcon = "@mipmap/ic_launcher_round" android:supportsRtl = "true" android:theme = "@style/Theme.OrientationChange" > < activity android:name = ".MainActivity" android:exported = "true" android:configChanges = "orientation|screenSize" > < intent-filter > < action android:name = "android.intent.action.MAIN" /> < category android:name = "android.intent.category.LAUNCHER" /> </ intent-filter > </ activity > </ application > </ manifest > |
Step 3: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. As such, no changes are required and kept untouched.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < androidx.constraintlayout.widget.ConstraintLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:app = "http://schemas.android.com/apk/res-auto" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".MainActivity" > < TextView android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "Hello Geek!" app:layout_constraintBottom_toBottomOf = "parent" app:layout_constraintLeft_toLeftOf = "parent" app:layout_constraintRight_toRightOf = "parent" app:layout_constraintTop_toTopOf = "parent" /> </ androidx.constraintlayout.widget.ConstraintLayout > |
Step 4: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
package org.w3wiki.orientationchange import android.content.res.Configuration import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.Toast class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_main) } // Listening to the orientation config // changes and generating Toast Messages override fun onConfigurationChanged(newConfig: Configuration) { super .onConfigurationChanged(newConfig) if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { Toast.makeText(baseContext, "Landscape Mode" , Toast.LENGTH_SHORT).show() } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { Toast.makeText(baseContext, "Portrait Mode" , Toast.LENGTH_SHORT).show() } } } // If you are developing for API level 13 (3.2) or higher, // you should specify android:configChanges="orientation|screenSize", // otherwise onConfigurationChanged won't be called. // https://developer.android.com/guide/topics/resources/runtime-changes#HandlingTheChange |
Output:
You can see that the program listens to orientation change and generates toast messages.