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. <Button
  12. android:id="@+id/button"
  13. android:layout_width="150dp"
  14. android:layout_height="wrap_content"
  15. android:layout_marginLeft="100dp"
  16. android:layout_marginTop="73dp"
  17. android:text="Button"
  18. android:onClick="onClick"
  19. />
  20. <EditText
  21. android:id="@+id/editText1"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:layout_centerHorizontal="true"
  25. android:ems="10" />
  26. </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. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.view.Menu;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. public class MainActivity extends Activity{
  10. EditText editText;
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_main);
  15. editText=(EditText)findViewById(R.id.editText);
  16. Button button=(Button)findViewById(R.id.button);
  17. button.setOnClickListener(new OnClickListener() {
  18. @Override
  19. public void onClick(View v) {
  20. editText.setText("O android");
  21. }
  22. });
  23. }
  24. }
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. <uses-sdk
  7. android:minSdkVersion="7"
  8. android:targetSdkVersion="16" />
  9. <application
  10. android:allowBackup="true"
  11. android:icon="@drawable/ic_launcher"
  12. android:label="@string/app_name"
  13. android:theme="@style/AppTheme" >
  14. <activity
  15. android:name="com.screenorientation.MainActivity"
  16. android:label="@string/app_name"
  17. android:screenOrientation="landscape"
  18. >
  19. <intent-filter>
  20. <action android:name="android.intent.action.MAIN" />
  21. <category android:name="android.intent.category.LAUNCHER" />
  22. </intent-filter>
  23. </activity>
  24. </application>
  25. </manifest>
Step 4
Landscape oriented image:
landscapeimage