Screen Orientations in Android with Examples - GeeksforGeeks

23 Feb, 2021
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 orientationThere 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.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.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.xmlactivity_next.xmlactivity_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> 
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.activity_next.xml: XML file for second activity consist of constraint layout with Text View in it. This activity is in Landscape state.
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.
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
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_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>  
<?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.MainActivity.java: Java file for Main Activity, in which setOnClick() listener is attached to the button to launch next activity with different orientation.NextActivity.java: Java file for Next Activity which is in Landscape mode.MainActivity.javaNextActivity.javaMainActivity.javapackage 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.javapackage 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);    }}
MainActivity.java: Java file for Main Activity, in which setOnClick() listener is attached to the button to launch next activity with different orientation.NextActivity.java: Java file for Next Activity which is in Landscape mode.
MainActivity.java: Java file for Main Activity, in which setOnClick() listener is attached to the button to launch next activity with different orientation.
NextActivity.java: Java file for Next Activity which is in Landscape mode.
MainActivity.java
NextActivity.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);    }}
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.xmlAndroidManifest.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>   
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:Activity 1:Activity 2:
Activity 1:Activity 2:
Activity 1:
Activity 2:
Android-Misc
Android
Java
Java
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Create and Add Data to SQLite Database in Android?
Broadcast Receiver in Android With Example
Android RecyclerView in Kotlin
CardView in Android With Example
Content Providers in Android with Example
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Reverse a string in Java
Arrays.sort() in Java with examples