Football Fans: Download the 2012 Schedule App from Google Play!


Go Back   Android Forums > Android Development > Application Development

Application Development Dev Lounge for the Coder Folks



View Poll Results: As far as mobile apps development are concerned which is best?
Android 0 0%
Iphone 0 0%
blackberry 0 0%
symbian 0 0%
Voters: 0. You may not vote on this poll

Reply
 
LinkBack Thread Tools
Old July 22nd, 2011, 05:08 AM   #1 (permalink)
New Member
 
Join Date: Jul 2011
Posts: 3
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Default Orientation Change not getting detected

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"
androidrientation="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>

anshulzunke is offline  
Reply With Quote
Sponsors
Old July 22nd, 2011, 06:15 AM   #2 (permalink)
Over Macho Grande?
 
alostpacket's Avatar
 
Join Date: Nov 2009
Location: NY
Posts: 7,090
 
Device(s): GalaxyNexus(LTE), NexusOne, OG Droid, GalaxyTab 10.1(LTE), Eris, Logitech Revue (fishtank)
Thanks: 4,164
Thanked 3,126 Times in 1,292 Posts
Default

does that code compile?

What's the error your are seeing?
alostpacket is offline  
Reply With Quote
Old July 25th, 2011, 12:01 AM   #3 (permalink)
New Member
 
Join Date: Jul 2011
Posts: 3
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Default Hi

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.
anshulzunke is offline  
Reply With Quote
Old July 25th, 2011, 11:52 AM   #4 (permalink)
Over Macho Grande?
 
alostpacket's Avatar
 
Join Date: Nov 2009
Location: NY
Posts: 7,090
 
Device(s): GalaxyNexus(LTE), NexusOne, OG Droid, GalaxyTab 10.1(LTE), Eris, Logitech Revue (fishtank)
Thanks: 4,164
Thanked 3,126 Times in 1,292 Posts
Default

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

Quote:
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
alostpacket is offline  
Reply With Quote
Reply

Bookmarks

Tags
android, orientation


Go Back   Android Forums > Android Development > Application Development User CP
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -5. The time now is 11:43 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Custom vBulletin Skins by: Relivo