Hi Friends,
I need a help. I was running few experiments on android and i got stuck in one of the experiments
The aim is detecting the change of orientation in the mobile ph so that we could take some action
the code is
OrientationActivity.java
package com.experimets;
import android.app.Activity;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.OrientationEventListener;
import android.widget.TextView;
import android.widget.Toast;
public class OrientationActivity extends Activity{
TextView textviewOrientation;
OrientationEventListener myOrientationEventListener;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textviewOrientation = (TextView)findViewById(R.id.textorientation);
myOrientationEventListener
= new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL){
@Override
public void onOrientationChanged(int arg0) {
// TODO Auto-generated method stub
textviewOrientation.setText("Orientation: " + String.valueOf(arg0));
}};
if (myOrientationEventListener.canDetectOrientation() ){
Toast.makeText(this, "Can DetectOrientation", Toast.LENGTH_LONG).show();
myOrientationEventListener.enable();
}
else{
Toast.makeText(this, "Can't DetectOrientation", Toast.LENGTH_LONG).show();
finish();
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
myOrientationEventListener.disable();
}
}
res/layout/Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android

rientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:id="@+id/textorientation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Orientation: "
/>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.experimets"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:configChanges="orientation" android:label="@string/app_name">
<activity android:name=".OrientationActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>