Quick Contact


    Screen Orientation

    The screen Orientation attribute of the operation variable is a property. Android operation may be in portrait, landscape, sensor, or undefined orientation. It must be defined in the AndroidManifest.xml format.

    In Android, the screen orientation attribute, also known as screen rotation, is an attribute of the operation feature. A configuration transition occurs as the screen direction changes from one state to another.

    Screen orientation states

    Any android application can have a variety of screen orientation states, including:

    Information about your activities.

    SCREEN ORIENTATION LANDSCAPE is a screen orientation landscape.

    Information about your activities.

    SCREEN ORIENTATION PORTRAIT is a screen orientation portrait.

    Information about your activities.

    • SCREEN ORIENTATION UNSPECIFIED SCREEN ORIENTATION UNSPECIFIED SCREEN ORIENTATION
    • SCREEN ORIENTATION USER is a property of ActivityInfo.
    • SCREEN ORIENTATION SENSOR is a property of ActivityInfo.
    • SCREEN ORIENTATION BEHIND is a property of ActivityInfo.
    • SCREEN ORIENTATION NOSENSOR is a property of ActivityInfo.
    • SCREEN ORIENTATION SENSOR LANDSCAPE is an ActivityInfo.SCREEN ORIENTATION SENSOR LANDSCAPE variable.
    Syntax:

    < activity android:name="package_name.Your_ActivityName"

    android:screenOrientation=”orirntation_type”>

    For Example:
    	< activity android:name=" Tutorials.ducatindia.com.screenorientation.MainActivity"  
    	android:screenOrientation="portrait">
    	< /activity>
    	< activity android:name=".SecondActivity"  
    	android:screenOrientation="landscape">
    	< /activity>
    	
    Screen orientation examples for Android in Portrait and Landscape modes

    We’ll make two tasks in this case, each with a screen sizes orientation. The first activity (MainActivity) will be oriented “portrait” and the secondary activity (SecondActivity) will be oriented “landscape.”

    activity_main.xml

    < ?xml version="1.0" encoding="utf-8"?>

    < android.support.constraint.ConstraintLayoutxmlns: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=”tutorials.ducatindia.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

    Making the Java file: There’ll be two operations, so two Java files will be created, one for each operation.

    MainActivity.java: Main Activity Java file, in which the setOnClick() listener is connected to the button to activate the next operation in a new orientation.

    NextActivity.java: This is the Java file for the Next Activity in Landscape mode.

    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);

    }

    }

    activity_second.xml
    File: activity_second.xml

    < ?xml version="1.0" encoding="utf-8"?>

    < android.support.constraint.ConstraintLayoutxmlns: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=”tutorials.ducatindia.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>

    Second Activity class
    File: SecondActivity.java

    package tutorials.ducatindia.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);

    }

    }

    AndroidManifest.xml

    Modifying the AndroidManifest file entails the following steps: Add the screenOrientation status of operation, as well as its orientation, to the AndroidManifest.xml format. We have a “portrait” orientation for MainActivity and a “landscape” inclination for NextActivity in this section.

    The code for the AndroidManifest file is as follows:

    File: AndroidManifest.xml

    Add the screen Orientation attribute to the operation in the AndroidManifest.xml file and specify its orientation. We include “portrait” alignment for Main Activity and “landscape” for Second Activity in this case.

    < ?xml version="1.0" encoding="utf-8"?>

    < manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package=”tutorials.ducatindia.com.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”>

    < activity

    android:name=”example.javatpoint.com.screenorientation.MainActivity”

    android:screenOrientation=”portrait”>

    < intent-filter>

    < action android:name="android.intent.action.MAIN" />

    < category android:name="android.intent.category.LAUNCHER" />

    < /intent-filter>

    < /activity>

    < activity android:name=".SecondActivity"

    android:screenOrientation=”landscape”>

    < /activity>

    < /application>

    < /manifest>

    Output:

    Screen Orientation

    Enroll Yourself in Live Classes For Android Training Online.

    Copyright 1999- Ducat Creative, All rights reserved.