• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Orientation Change not getting detected

As far as mobile apps development are concerned which is best?

  • Android

    Votes: 0 0.0%
  • Iphone

    Votes: 0 0.0%
  • blackberry

    Votes: 0 0.0%
  • symbian

    Votes: 0 0.0%

  • Total voters
    0
  • Poll closed .
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:orientation="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>
 
Yeah the code was very much running.
I found the problem. I was running the code on emulator and every time i change the orientation, the activity gets recreated so its not calling the call back function rather it calls the Activity create method.

So everytime the activity gets recreated, we can get the orientation.

Still if you could tell me bit more about orientation change etc it would be of great help in enhancing my knowledge about android platform as i am very immature programmer right now on this platform.
 
Thanks for posting back the solution :) always helps people in this forum when people come back even if they solved the problem :)

Also, I do not know much about orientation but I think you probably have 2 options:

1) use onConfigurationChanged and handle the orientation change yourself. This would get called only when the seson has detected the orientation changed enough to need to re-orient the screen for the user. If you put this in the XML:
Code:
<activity 
android:name=".MyActivity"
          android:configChanges="orientation|keyboardHidden"
          android:label="@string/app_name"
>
This will tell the OS not to restart your activity and instead it will call
onConfigurationChanged(Config newConfig); in your activity

Activity | Android Developers

public void onConfigurationChanged (Configuration newConfig)

Since: API Level 1

Called by the system when the device configuration changes while your activity is running. Note that this will only be called if you have selected configurations you would like to handle with the configChanges attribute in your manifest. If any configuration change occurs that is not selected to be reported by that attribute, then instead of reporting it the system will stop and restart the activity (to have it launched with the new configuration).
At the time that this function has been called, your Resources object will have been updated to return resource values matching the new configuration.
2) You can run a Thread that checks the orientation sensor for the values evey X number of milliseconds. For example you could run a thread that looks at the sensor data every 500 milliseconds. Threads are a big topic though, so a lot to learn there. However, knowing how to properly use threads is essential to becomming a good Android programmer (in my opinion ;) )

Some articles about threading that might help:

Android Developers Blog: Painless threading

Threading with Android - part 1 | Android Academy
 
Back
Top Bottom