Screen Orientation

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Android Screen Orientation Example

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. For example:

  1.  

  2.             android:name="com.example.screenorientation.MainActivity"  

  3.             android:label="@string/app_name"   

  4.             android:screenOrientation="landscape"  

  5.             >  

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 landscape mode screen orientation example

activity_main.xml

File: activity_main.xml
 
  1. <RelativeLayout xmlns:androclass="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.     tools:context=".MainActivity" >  
  10.   
  11.     <Button  
  12.         android:id="@+id/button1"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_marginLeft="66dp"  
  16.         android:layout_marginTop="73dp"  
  17.         android:text="Button"  
  18.         android:onClick="onClick"  
  19.          />  
  20.   
  21.     <EditText  
  22.         android:id="@+id/editText1"  
  23.         android:layout_width="wrap_content"  
  24.         android:layout_height="wrap_content"  
  25.         android:layout_centerHorizontal="true"  
  26.         android:ems="10" />  
  27.   
  28. RelativeLayout>  

Activity class

 

File: MainActivity.java
 
  1. package com.example.f;  
  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.     EditText editText1;  
  13.     Button button1;  
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.           
  19.         editText1=(EditText)findViewById(R.id.editText1);  
  20.         button1=(Button)findViewById(R.id.button1);  
  21.     }  
  22.     public void onClick(View v) {  
  23.         editText1.setText("O android");  
  24.     }  
  25. }  

AndroidManifest.xml

File: AndroidManifest.xml
 
  1. xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:androclass="http://schemas.android.com/apk/res/android"  
  3.     package="com.example.screenorientation"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  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.example.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.   
  29. manifest>  

android screen orientation example output 1