How to Change the Screen Orientation Programmatically using a Button in Android?
Last Updated :
14 Oct, 2020
Generally, the screen orientation of any application is Portrait styled. But when it comes to gaming or any other multimedia service such as watching a video, the screen orientation must change functionally from Portrait to landscape or vice-versa when the functionality is not required. So a developer has to explicitly write a program where the orientation changes without the user to invoke it traditionally by switching on and off the rotate/auto-rotate available in the swipe-down menu.
The main focus of changing the screen to landscape mode is to increase the area of action and response, and back to normal when not required. The latter can be neglected if there is a direct exit from the application. So this is not an application, instead, it is a feature, that can be given to any application. Some of the applications are where this feature can be added are:
- Gaming Applications: As previously mentioned. Landscape view gives better accessibility to the screen for touch in Gaming applications.
- Videos Application: Applications broadcasting multimedia such as a video can enable this feature to view the same video by utilizing each screen's pixel.
- Photo Editing Applications: This feature can help the user view the changes and compare two different portions of the image as the display size has now increased.
- Applications requiring multiple user inputs: such as a notepad, or a browser, where a user can read the entire line or paragraphs that are being written or applications.
In this application, we will create this feature and keep it explicit, i.e., a Button should be clicked to make the changes. The code in the latter section of the article is limited to the created application only. Screen Orientation changed inside the application make no changes outside the application, i.e., the orientation outside the application remains the same as previously. Note that we are going to implement this project using the Kotlin language.
Steps for Changing the Screen Orientation Programmatically using a Button
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Step 2: Working with the activity_main.xml file
When the setup is ready, go to the activity_main.xml file, which represents the UI of the project. Create a Button that changes the screen orientation on click. Below is the code for the activity_main.xml file.
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="click" />
</RelativeLayout>
Step 3: Working with the MainActivity.kt file
In the MainActivity.kt file, declare the Button to change screen orientation (refer to the codes). While setting the onClickListeners to the Button, orientation request is sent to the ActivityInfo. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
import android.content.pm.ActivityInfo
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btn = findViewById<Button>(R.id.btn)
// reference to change the orientation on every click
var isPortrait = true
// Button action on click
btn.setOnClickListener {
// if isPortrait true, change to Landscape
requestedOrientation = if (isPortrait) {
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
// else change to Portrait
} else {
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}
// opposite the value of isPortrait
isPortrait = !isPortrait
}
}
}
Output: Run on Emulator
Similar Reads
Android Jetpack Compose - Change the Screen Orientation Programmatically using a Button Many times in android applications we have to programmatically change the screen orientation to perform some task. If we want to play a video in landscape mode then we have to switch to landscape mode within our android application. So we have to change screen orientation in our android application
4 min read
How to Increase/Decrease Screen Brightness in Steps Programmatically in Android? Screen brightness is one such factor that directly affects the Users as well as the Battery on a device. Android devices are Smart systems and have an inbuilt system for Auto-Brightness. But mostly this feature is unchecked by the users or set off by default. Irrespective of whether this feature is
5 min read
How to Increase/Decrease Screen Brightness using Volume Keys Programmatically in Android? Screen brightness is one such factor that directly affects the Users as well as the Battery on a device. Android devices are Smart systems and have an inbuilt system for Auto-Brightness. But mostly this feature is unchecked by the users or set off by default. Irrespective of whether this feature is
5 min read
How to Change TextView Size Programmatically in Android? A TextView in Android is a UI element to display text. It can be programmed in the layout file statically as well as in the main code dynamically. Thus, various attributes of a TextView such as the text, text color, text size, TextView background, and its size can be changed programmatically. In thi
3 min read
How to Change the Whole App Language in Android Programmatically? Android 7.0(API level 24) provides support for multilingual users, allowing the users to select multiple locales in the setting. A Locale object represents a specific geographical, political, or cultural region. Operations that require these Locale to perform a task are called locale-sensitive and u
5 min read
How to Programmatically Take a Screenshot on Android? Ever wanted to take some perfect screenshot of a particular view, or perhaps some UI element ruined your favorite screenshot? Don't worry much, this Geeks for Geeks article will help you achieve it by making an app from scratch. As Below is the code for the title name in this article, we are going t
3 min read
How to Change App Icon of Android Programmatically in Android? In this article, we are going to learn how to change the App Icon of an App on the Button Click. This feature can be used when we have an app for different types of users. Then as per the user type, we can change the App Icon Dynamically. Step by Step Implementation Step 1: Create a New Project To
3 min read
How to Find the Screen Resolution of a Device Programmatically in Android? Screen Resolution refers to the number of pixels on display. A higher resolution means more pixels and more pixels provide the ability to display more visual information. This entity is widely used in applications related to the broadcasting of real-time visuals such as live video, gaming, etc for o
3 min read
Different Ways to Programmatically Restart an Android App on Button Click Sometimes we want to refresh an Activity, but there it is not possible to refresh. In that place, if we restart our app then it automatically gets refreshed. Also, we can use this to restart our app whenever it crashes. Most of the time when we open any app then it fetches all the data currently ava
3 min read
How to Take Screenshot Programmatically in Android? In every android phone, we have feature to take screenshots of screens. In this article, we are going to explain how to take screenshots programmatically. Step-by-Step ImplementationStep 1: Create a New ProjectTo create a new project in Android Studio please refer to How to Create/Start a New Projec
4 min read