Monday, 2 March 2015

Get Device Orientation and use it inside the application

Android powered device has sensors to detects the current orientation of the device. The Orientation can be detected using single sensor or combination of sensors like Gravity, using Accelerometer and Magnetometer, Gravity and Magnetometer, Rotation etc. Determining the orientation of a device is very useful to many applications like games.

Android also provides an API OrientationEventListener, using which it is very easy to detect the device orientation. This is a helper class for receiving notifications from the SensorManager when the orientation of the device has changed. Let us create a simple demo application using this class.

Limitation: When device is lying flat on something like table, angle it notify is -1. So, you can’t rotate the device about the axis vertical to device screen when lying flat on a table.

In the demo application, there is an imageview and textview in the main layout file. Text view displays the value of orientation in angle and image is rotated accordingly. In the sample application, OrientationEventListener is enabled inside onResume method and is disabled in onPause life cycle method of the main activity.

orientation1.png
orientation2.png

MainActivity.java

package test.example.orientationdetection;

import android.app.Activity;
import android.os.Bundle;
import android.view.OrientationEventListener;
import android.widget.ImageView;
import android.widget.TextView;


public class MainActivity extends Activity {

ImageView imageView;
TextView angle;
OrientationEventListener orientationListener;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       
       imageView=(ImageView)findViewById(R.id.imageView1);
       angle=(TextView)findViewById(R.id.angle_textView);
       
       orientationListener = new OrientationEventListener(this) {
@Override
public void onOrientationChanged(int orientation) {
angle.setText("Current Angle : "+orientation);
imageView.setRotation(orientation);
}
};
   }

   @Override
   protected void onResume() {    
    super.onResume();
   
    if(orientationListener.canDetectOrientation()){
    orientationListener.enable();
    }
   }
   
   @Override
   protected void onPause() {    
    super.onPause();
   
    if(orientationListener.canDetectOrientation()){
    orientationListener.disable();
    }
   }
  
}

activity_main.xml

<RelativeLayout 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"
   android:textAlignment="center"
   tools:context="test.example.orientationdetection.MainActivity" >

   <TextView
       android:id="@+id/textView1"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_alignParentRight="true"
       android:text="Device Orientation Test "
       android:textSize="20sp" />

   <TextView
       android:id="@+id/angle_textView"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@+id/textView1"
       android:layout_centerHorizontal="true"
       android:layout_marginTop="67dp"
       android:text=""
       android:textAppearance="?android:attr/textAppearanceMedium" />

   <ImageView
       android:id="@+id/imageView1"
       android:layout_width="100dp"
       android:layout_height="100dp"
       android:layout_centerHorizontal="true"
       android:layout_centerVertical="true"
       android:src="@drawable/micro_black" />

</RelativeLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="test.example.orientationdetection"
   android:versionCode="1"
   android:versionName="1.0" >

   <uses-sdk
       android:minSdkVersion="14"
       android:targetSdkVersion="19" />a

   <application
       android:allowBackup="true"
       android:icon="@drawable/ic_launcher"
       android:label="@string/app_name"
       android:theme="@style/AppTheme" >
       <activity
           android:name=".MainActivity"
           android:label="@string/app_name"
           android:screenOrientation="portrait" >
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />

               <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
       </activity>
   </application>

</manifest>

No comments:

Post a Comment