Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Screen Orientations in Android with Examples
Next article icon

Screen Orientations in Android with Examples

Last Updated : 23 Feb, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report
Screen Orientation, also known as screen rotation, is the attribute of activity element in android. When screen orientation change from one state to other, it is also known as configuration change. States of Screen orientation There are various possible screen orientation states for any android application, such as:
  • ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
  • ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
  • ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
  • ActivityInfo.SCREEN_ORIENTATION_USER
  • ActivityInfo.SCREEN_ORIENTATION_SENSOR
  • ActivityInfo.SCREEN_ORIENTATION_BEHIND
  • ActivityInfo.SCREEN_ORIENTATION_NOSENSOR
  • ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
  • ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT
  • ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT
The initial orientation of the Screen has to be defined in the AndroidManifest.xml file. Syntax: AndroidManifest.xml
<activity android:name="package_name.Your_ActivityName"       android:screenOrientation="orientation_type">   </activity>   
Example:
      android:screenOrientation="orientation_type">  
How to change Screen orientation? Here is an example of an Android application that changes screen orientation for Landscape and Portrait mode. We will create two activities of different screen orientation.
  • The first activity will be as "portrait" orientation and
  • Second activity as "landscape" orientation state.
Step-by-Step demonstration:
  • Creating the activities: There will be two activities and hence two XML files, one for each activity.
    1. activity_main.xml: XML file for first activity consist of constraint layout with Button and Text View in it. This activity is in Landscape state.
    2. activity_next.xml: XML file for second activity consist of constraint layout with Text View in it. This activity is in Landscape state.
    Below is the code for both activities: activity_main.xml
    <?xml version="1.0" encoding="utf-8"?>   <!--Constraint Layout--> <android.support.constraint.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="com.geeksforgeeks.screenorientation.MainActivity">     <!--Button to launch next activity with onClick-->       <Button           android:id="@+id/b1"           android:layout_width="wrap_content"           android:layout_height="wrap_content"         android:text="Next Activity"             android:layout_marginTop="100dp"           android:onClick="onClick"         android:layout_marginBottom="10dp"            app:layout_constraintBottom_toBottomOf="parent"           app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintVertical_bias="0.613"           app:layout_constraintHorizontal_bias="0.612"           app:layout_constraintStart_toStartOf="parent"           app:layout_constraintTop_toBottomOf="@+id/tv1"            />      <!--TextView -->     <TextView           android:text="Potrait orientation"           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:layout_centerHorizontal="true"            android:layout_marginTop="124dp"         android:textSize="25dp"           app:layout_constraintEnd_toEndOf="parent"           app:layout_constraintHorizontal_bias="0.502"           app:layout_constraintStart_toStartOf="parent"           app:layout_constraintTop_toTopOf="parent" />   </android.support.constraint.ConstraintLayout>   
    activity_next.xml
    <?xml version="1.0" encoding="utf-8"?>    <!--Constraint layout-->   <android.support.constraint.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_height="match_parent"       android:layout_width="match_parent"         tools:context="com.geeksforgeeks.screenorientation.NextActivity">        <!--TextView-->       <TextView           android:id="@+id/tv"           android:text="Landscape orientation"         android:layout_width="wrap_content"           android:layout_height="wrap_content"             android:layout_marginTop="170dp"             android:textSize="22dp"           app:layout_constraintStart_toStartOf="parent"          app:layout_constraintEnd_toEndOf="parent"           app:layout_constraintHorizontal_bias="0.502"           app:layout_constraintTop_toTopOf="parent" />   </android.support.constraint.ConstraintLayout>  
  • Creating the Java file: There will be two activities and hence two Java files, one for each activity.
    1. MainActivity.java: Java file for Main Activity, in which setOnClick() listener is attached to the button to launch next activity with different orientation.
    2. NextActivity.java: Java file for Next Activity which is in Landscape mode.
    MainActivity.java
    package com.geeksforgeeks.screenorientation;  import android.support.v7.app.AppCompatActivity; import android.content.Intent; import android.view.View; import android.widget.Button;  public class MainActivity extends AppCompatActivity {      // declare button variable     Button button;      @Override     protected void onCreate(Bundle savedInstanceState)     {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          // initialise button with id         button = findViewById(R.id.b1);     }      // onClickListener attached to button     // to send intent to next activity     public void onClick(View v)     {         // Create instance of intent and         // startActivity with intent object         Intent intent             = new Intent(                 MainActivity.this,                 NextActivity.class);         startActivity(intent);     } } 
    NextActivity.java
    package com.geeksforgeeks.screenorientation;  import android.support.v7.app.AppCompatActivity;  public class MainActivity extends AppCompatActivity {      @Override     protected void onCreate(Bundle savedInstanceState)     {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);     } } 
  • Updating the AndroidManifest file: In AndroidManifest.xml file, add the screenOrientation state in activity along with its orientation. Here, we provide "portrait" orientation for MainActivity and "landscape" for NextActivity. Below is the code for AndroidManifest file: AndroidManifest.xml
    <?xml version="1.0" encoding="utf-8"?>   <manifest xmlns:android="http://schemas.android.com/apk/res/android"       package="com.geeksforgeeks.screenorientation">          <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/AppTheme">          <!-Define potrait orientation for Main activity-->           <activity               android:name="com.geeksforgeeks.screenorientation.MainActivity"               android:screenOrientation="portrait">               <intent-filter>                   <action android:name="android.intent.action.MAIN" />                   <category android:name="android.intent.category.LAUNCHER" />               </intent-filter>           </activity>          <!--Define landscape orientation for NextActivity-->          <activity android:name=".NextActivity"               android:screenOrientation="landscape">           </activity>       </application>   </manifest>    
  • Output:
    1. Activity 1:
    2. Activity 2:

Next Article
Screen Orientations in Android with Examples

R

Rishabh007
Improve
Article Tags :
  • Java
  • Android
  • Android-Misc
Practice Tags :
  • Java

Similar Reads

    TextView in Android with Example
    TextView is a simple widget that is seen in every android application. This widget is used to display simple text within the android application. We can add custom styling to the text that we have to show. In this article, we will take a look at How to create a simple Text View in an android applica
    2 min read
    TextView widget in Android with Examples
    Widget refers to the elements of the UI (User Interface) that help the user interact with the Android App. TextView is one of many such widgets which can be used to improve the UI of the app. TextView refers to the widget which displays some text on the screen based on the layout, size, colour, etc
    5 min read
    How to Use FFmpeg in Android with Example?
    FFmpeg, short for Fast-forward MPEG, is a free and open-source multimedia framework, which is able to decode, encode, transcode, mux, demux, stream, filter and play fairly all kinds of multimedia files that have been created to date. It also supports some of the eldest formats. FFmpeg compiles and r
    15+ min read
    Android ListView in Java with Example
    A ListView in Android is a type of AdapterView that displays a vertically scrollable list of items, with each item positioned one below the other. Using an adapter, items are inserted into the list from an array or database efficiently. For displaying the items in the list method setAdaptor() is use
    3 min read
    Android Sensors with Example
    In our childhood, we all have played many android games like Moto Racing and Temple run in which by tilting the phone the position of the character changes. So, all these happen because of the sensors present in your Android device. Most Android-powered devices have built-in sensors that measure mot
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences