- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can I check if the Android phone is in Landscape or Portrait?
Introduction
Many times while launching a specific screen for an android application, we have to check the orientation of the device whether the device is in portrait mode or a landscape mode. In this article we will take a look at How we can check if the Android phone is in Landscape mode or a portrait mode.
Implementation
We will be creating a simple application in which we will be displaying two text views. First text view we will be using it to display the heading of our application. In the second text view we will be displaying the current orientation mode of our device whether the device is in portrait mode or a landscape mode.
Step 1 : Creating a new project in Android Studio
Navigate to Android studio as shown in below screen. In the below screen click on New Project to create a new Android Studio Project.

After clicking on New Project you will get to see the below screen.

Inside this screen we have to simply select Empty Activity and click on Next. After clicking on next you will get to see the screen below.

Inside this screen we have to simply specify the project name. Then the package name will be generated automatically.
Note − Make sure to select the Language as Java.
After specifying all the details click on Finish to create a new Android studio project.
Once our project has been created we will get to see 2 files which are open i.e activity_main.xml and MainActivity.java file.
Step 2 : Working with activity_main.xml
Navigate to activity_main.xml. If this file is not visible. To open this file. In the left pane navigate to app>res>layout>activity_main.xml to open this file. After opening this file. Add the below code to it. Comments are added in the code to get to know in detail.
<?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"> <!-- creating a text view on below line--> <TextView android:id="@+id/idTVHeading" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_marginStart="20dp" android:layout_marginTop="20dp" android:layout_marginEnd="20dp" android:layout_marginBottom="20dp" android:padding="4dp" android:text="Implementing Services in Android" android:textAlignment="center" android:textColor="@color/black" android:textSize="20sp" android:textStyle="bold" /> <!-- creating a text view to display current mode --> <TextView android:id="@+id/idTVMode" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/idTVHeading" android:layout_margin="4dp" android:padding="4dp" android:text="Mode" android:textAlignment="center" android:textAllCaps="false" android:textColor="@color/black" android:textSize="20sp" android:textStyle="bold" /> </RelativeLayout>
Explanation : In the above code we are creating a root layout as a Relative Layout. Inside this layout we are creating a text view which is used to display the heading of our application. After that we are creating one more text view in which we will be displaying the current mode for our device whether the device is present in landscape mode or a portrait mode.
Step 3: Working with MainActivity.java file
Navigate to MainActivity.java. If this file is not visible. To open this file. In the left pane navigate to app>res>layout>MainActivity.java to open this file. After opening this file. Add the below code to it. Comments are added in the code to get to know in detail.
package com.example.java_test_application; import android.content.Intent; import android.content.res.Configuration; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { // creating variables on below line for text view. private TextView modeTV; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // initializing variables on below line. modeTV = findViewById(R.id.idTVMode); // on below line checking for the mode of the device if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { // on below line setting text message according to the device mode modeTV.setText("Landscape Mode Activated.."); } else { // on below line setting text message according to the device mode modeTV.setText("Potrait Mode Activated.."); } } }
Explanation: In the above code firstly we are creating variables for our text view. Now we will get to see the onCreate method. This is the default method of every android application. This method is called when the application view is created. Inside this method we are setting the content view i.e the layout file named activity_main.xml to set the UI from that file. Inside the onCreate method we are initializing the text view variable with the id which we have given in our activity_main.xml file. After that we are checking the orientation of our device whether it is portrait or landscape. If the orientation is landscape we are setting a text message to our text view. Similarly we are updating the text view for the portrait mode as well.
After adding the above code now we have to simply click on the green icon in the top bar to run our application on a mobile device.
Note − Make sure you are connected to your real device or emulator.
Output

Conclusion
In the above article we have taken a look on How we can check if the Android phone is in landscape mode or a portrait mode.