How to set screen orientation in android studio with example

In this tutorial, we are going to learn. How to set screen orientation in android studio. Follow this guide step by step.

As we know, Android mobiles support two types of screen orientation: Landscape and portrait. Whenever the screen orientation is changed. The current activity will destroy and automatically recreate the activity in the current orientation. In other words, the onCreate() method of the activity is fired whenever there is a change in screen orientation.

Portrait orientation: This type of orientation is larger in height and smaller in width. Or we can say, Height is always greater than the width.

Landscape orientation: This type of orientation is smaller in height and larger in width. Or we can say, Width is always greater than the height.

How to set screen orientation in android

Screen Orientation is an attribute of the activity element. The value of the screen orientation may be portrait, landscape, sensor, unspecified, locked, fullsensor, etc.

The syntax for screen orientation: 

<activity android:name=".LandscapeActivity"
            android:screenOrientation="landscape"></activity>

Screen Orientation example in android

In this manifest.xml file, There is two Activity one is MainActivity, and the other is LandscapeActivity. Under MainActivity, we set the screen orientation value is a portrait. Under LandscapeActivity, value is landscape.  We changed the current activity with another activity using Intent.

Read more: 

Android_Manifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.demoscreenorientation">

    <application
        android:allowBackup="true"
        android:icon="@drawable/coding_query"
        android:label="@string/app_name"
        android:roundIcon="@drawable/coding_query"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".LandscapeActivity"
            android:screenOrientation="landscape"></activity>
        <activity android:name=".MainActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
activity_main.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">

    <TextView
        android:id="@+id/tvPotrait"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="Potrait mode!"
        android:textSize="50sp" />

    <Button
        android:id="@+id/btLandscape"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="30dp"
        android:gravity="center"
        android:text="Landscape" />


</RelativeLayout>
MainActivity.java
package com.example.demoscreenorientation;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().hide();                       //Hide Title Bar
        Button btLandscape;
        btLandscape = findViewById(R.id.btLandscape);

        btLandscape.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent(MainActivity.this,LandscapeActivity.class);
                startActivity(intent);
            }
        });

    }

}
activity_landscape.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=".LandscapeActivity">

    <TextView
        android:id="@+id/tvPotrait"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="This is Landscape mode!"
        android:textSize="50sp" />

</RelativeLayout>
LandscapeActivity.java
package com.example.demoscreenorientation;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Window;

public class LandscapeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_landscape);
        getSupportActionBar().hide();

    }
}

Output :

 

Some value passed to the screen orientation attribute

portraitThe display is taller than it is wide.
landscapeThe display is Widder than it is tall.
unspecifiedDefault Orientation.
lockedLocks the orientation to its current rotation.
reversePortraitPortrait orientation in the opposite direction from normal Portrait.
reverseLandscapeLandscape orientation in the opposite direction from the normal landscape.
sensorThe orientation is determined by the device orientation sensor.
userThe user’s current preferred orientation.

and many more are present there.

Download the code :

If you like this tutorial, How to set screen orientation in android studio with example. Follow us on Facebook and Telegram. For more information comment below.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *