Screen Orientation in Android Using Android Studio

Introduction

 
This article explains screen orientation in Android. Android Studio is used to create the sample.
 
The screen orientation attribute is provided by the activity element in the Android Manifest.Xml file. The orientations provided by the activity are Portrait, Landscape, Sensor, Unspecified and so on. To perform a screen orientation activity you define the properties in the Android Manifest.Xml file.
 
Example
  1. <activity  
  2.             android:name="com.screenorientation.MainActivity"  
  3.             android:label="@string/app_name"  
  4.             android:screenOrientation="portrait">    
  • Portrait: In a portrait mode the screen will be taller, not wider.
  • LandScape: In a landscape mode the screen will be wider, not taller.
  • Sensor: This orientation will be determined depending on the device sensor. 
  • Unspecified: For this, the orientation will be chosen by the system.
Step 1
 
Create an XML file and use a button and a textView as in the following:
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     android:background="#123456"  
  10.     tools:context=".MainActivity" >  
  11.    
  12.     <Button  
  13.         android:id="@+id/button"  
  14.         android:layout_width="150dp"  
  15.         android:layout_height="wrap_content"  
  16.         android:layout_marginLeft="100dp"  
  17.         android:layout_marginTop="73dp"  
  18.         android:text="Button"  
  19.         android:onClick="onClick"  
  20.         />  
  21.    
  22.     <EditText  
  23.         android:id="@+id/editText1"  
  24.         android:layout_width="wrap_content"  
  25.         android:layout_height="wrap_content"  
  26.         android:layout_centerHorizontal="true"  
  27.         android:ems="10" />  
  28.    
  29. </RelativeLayout> 
Step 2
 
Create a Java class file.
 
In it, you will create the id of a Button and EditText. After this set the button's click event handler and on a button click you will set the text of edittext to Android.
  1. package com.screenorientation;  
  2.    
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.EditText;  
  10.    
  11. public class MainActivity extends Activity{  
  12.    
  13.     EditText editText;  
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.    
  19.          editText=(EditText)findViewById(R.id.editText);  
  20.         Button button=(Button)findViewById(R.id.button);  
  21.    
  22.     button.setOnClickListener(new OnClickListener() {  
  23.         @Override  
  24.         public void onClick(View v) {  
  25.                 editText.setText("O android");   
  26.         }  
  27.     });  
  28. }  
Step 3
 
Android Manifest.Xml file
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.screenorientation"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.    
  7.     <uses-sdk  
  8.         android:minSdkVersion="7"  
  9.         android:targetSdkVersion="16" />  
  10.    
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.screenorientation.MainActivity"  
  18.             android:label="@string/app_name"  
  19.             android:screenOrientation="landscape"  
  20.            >  
  21.             <intent-filter>  
  22.                 <action android:name="android.intent.action.MAIN" />  
  23.    
  24.                 <category android:name="android.intent.category.LAUNCHER" />  
  25.             </intent-filter>  
  26.         </activity>  
  27.     </application>  
  28. </manifest> 
Step 4
 
Landscape oriented image:
 
landscapeimage