Apr 25, 2014

Set/get orientation programmatically

To set orientation programmatically with Java code, by calling setRequestedOrientation() method with parameter of ActivityInfo.SCREEN_ORIENTATION_PORTRAIT or ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE. To get current requested orientation, call getRequestedOrientation(), it will return either the orientation requested in its component's manifest, or the last requested orientation given to setRequestedOrientation(int)

Example:
Set orientation programmatically
Set orientation programmatically

package com.example.androidorientation;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
 
 Button btnSetPortrait, btnSetLandscape;
 TextView textOrientation;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  textOrientation = (TextView)findViewById(R.id.orientation);
  String currentOrientation = getCurrentOrientation();
  textOrientation.setText(currentOrientation);
  
  btnSetPortrait = (Button)findViewById(R.id.setPortrait);
  btnSetPortrait.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
   }});
  
  btnSetLandscape = (Button)findViewById(R.id.setLandscape);
  btnSetLandscape.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
   }});
  
  Toast.makeText(MainActivity.this, "onCreate()", Toast.LENGTH_LONG).show();
  
 }
 
 @Override
 protected void onDestroy() {
  super.onDestroy();
  Toast.makeText(MainActivity.this, "onDestroy()", Toast.LENGTH_LONG).show();
 }
 
 private String getCurrentOrientation(){
  String ori;
  
  //get the current orientation
  int currentOrientation = getRequestedOrientation();
  switch(currentOrientation){
  case ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED:
   ori = "SCREEN_ORIENTATION_UNSPECIFIED";
   break;
  case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
   ori = "SCREEN_ORIENTATION_LANDSCAPE";
   break;
  case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
   ori = "SCREEN_ORIENTATION_PORTRAIT";
   break;
  case ActivityInfo.SCREEN_ORIENTATION_USER:
   ori = "SCREEN_ORIENTATION_USER";
   break;
  case ActivityInfo.SCREEN_ORIENTATION_BEHIND:
   ori = "SCREEN_ORIENTATION_BEHIND";
   break;
  case ActivityInfo.SCREEN_ORIENTATION_SENSOR:
   ori = "SCREEN_ORIENTATION_SENSOR";
   break;
  case ActivityInfo.SCREEN_ORIENTATION_NOSENSOR:
   ori = "SCREEN_ORIENTATION_NOSENSOR";
   break;
  case ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE:
   ori = "SCREEN_ORIENTATION_SENSOR_LANDSCAPE";
   break;
  case ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT:
   ori = "SCREEN_ORIENTATION_SENSOR_PORTRAIT";
   break;
  case ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE:
   ori = "REEN_ORIENTATION_REVERSE_LANDSCAPE";
   break;
  case ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT:
   ori = "SCREEN_ORIENTATION_REVERSE_PORTRAIT";
   break;
  case ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR:
   ori = "SCREEN_ORIENTATION_FULL_SENSOR";
   break;
  case ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE:
   ori = "SCREEN_ORIENTATION_USER_LANDSCAPE";
   break;
  case ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT:
   ori = "SCREEN_ORIENTATION_USER_PORTRAIT";
   break;
  case ActivityInfo.SCREEN_ORIENTATION_FULL_USER:
   ori = "SCREEN_ORIENTATION_FULL_USER";
   break;
  case ActivityInfo.SCREEN_ORIENTATION_LOCKED:
   ori = "SCREEN_ORIENTATION_LOCKED";
   break;
  default:
   ori = "unknown!";
  }

  return ori;
 }

}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.androidorientation.MainActivity"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android-coding.blogspot.com" />
    <Button
        android:id="@+id/setPortrait"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Portrait" />
    <Button
        android:id="@+id/setLandscape"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Landscape" />
    <TextView
        android:id="@+id/orientation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

No comments:

Post a Comment

Infolinks In Text Ads