Dark Mode
Image

Android Fragments

Android Service

Android AlarmManager

Camera Tutorial

Sensor Tutorial

Android Graphics

Android Animation

Android Web Service

Android MCQ

Android Quiz

Screen Orientation

The screenOrientation is the attribute of activity element. The orientation of android activity can be portrait, landscape, sensor, unspecified etc. You need to define it in the AndroidManifest.xml file.

Syntax:

<activity android:name="package_name.Your_ActivityName"  
      android:screenOrientation="orirntation_type">  
</activity> 

Example:

<activity android:name=" example.javatpoint.com.screenorientation.MainActivity"  
     android:screenOrientation="portrait">  
</activity>  

 

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

The common values for screenOrientation attribute are as follows:

Value Description
unspecified It is the default value. In such case, system chooses the orientation.
portrait taller not wider
landscape wider not taller
sensor orientation is determined by the device orientation sensor.

Android Portrait and Landscape mode screen orientation example

In this example, we will create two activities of different screen orientation. The first activity (MainActivity) will be as "portrait" orientation and second activity (SecondActivity) as "landscape" orientation type.

activity_main.xml

File: activity_main.xml

<?xml version="1.0" encoding="utf-8"?>  
<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="example.javatpoint.com.screenorientation.MainActivity">  
  
  
    <Button  
        android:id="@+id/button1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_marginBottom="8dp"  
        android:layout_marginTop="112dp"  
        android:onClick="onClick"  
        android:text="Launch next activity"  
        app:layout_constraintBottom_toBottomOf="parent"  
        app:layout_constraintEnd_toEndOf="parent"  
        app:layout_constraintHorizontal_bias="0.612"  
        app:layout_constraintStart_toStartOf="parent"  
        app:layout_constraintTop_toBottomOf="@+id/editText1"  
        app:layout_constraintVertical_bias="0.613" />  
  
    <TextView  
        android:id="@+id/editText1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_centerHorizontal="true"  
        android:layout_marginEnd="8dp"  
        android:layout_marginStart="8dp"  
        android:layout_marginTop="124dp"  
        android:ems="10"  
        android:textSize="22dp"  
        android:text="This activity is portrait orientation"  
        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 class

File: MainActivity.java

package example.javatpoint.com.screenorientation;  
  
import android.content.Intent;  
import android.support.v7.app.AppCompatActivity;  
import android.os.Bundle;  
import android.view.View;  
import android.widget.Button;  
  
public class MainActivity extends AppCompatActivity {  
  
    Button button1;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
  
        button1=(Button)findViewById(R.id.button1);  
    }  
    public void onClick(View v) {  
        Intent intent = new Intent(MainActivity.this,SecondActivity.class);  
        startActivity(intent);  
    }  
}  

SecondActivity class

File: SecondActivity.java

<?xml version="1.0" encoding="utf-8"?>  
<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="example.javatpoint.com.screenorientation.SecondActivity">  
  
    <TextView  
        android:id="@+id/textView"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_marginEnd="8dp"  
        android:layout_marginStart="8dp"  
        android:layout_marginTop="180dp"  
        android:text="this is landscape orientation"  
        android:textSize="22dp"  
        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>

AndroidManifest.xml

File: AndroidManifest.xml

In AndroidManifest.xml file add the screenOrientation attribute in activity and provides its orientation. In this example, we provide "portrait" orientation for MainActivity and "landscape" for SecondActivity.
package example.javatpoint.com.screenorientation;  
  
import android.support.v7.app.AppCompatActivity;  
import android.os.Bundle;  
  
public class SecondActivity extends AppCompatActivity {  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_second);  
  
    }  
}  

Output:

android screen orientation example output 1 android screen orientation example output 2

Comment / Reply From