2

I'm new to android, I encountered this problem while learning the basics.

I've searched for my problem in google and here but can't find similiar one.

My problem is I can't detect when the screen orientation changed to landscape.

Here's the code I got from here

public void onConfigurationChanged(Configuration newConfig)
{
    super.onConfigurationChanged(newConfig);

    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
        System.out.println("orientation landscape");
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
        System.out.println("orientation potrait");
    } else{
        Toast.makeText(this, "undefined", Toast.LENGTH_SHORT).show();
        System.out.println("orientation undefined");
    }
}

    <activity android:name=".Hello2011Activity"
              android:label="@string/app_name"
              android:theme="@style/testem"
              android:configChanges="orientation"
              >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Here's the log

07-27 16:19:11.935: INFO/ActivityManager(52): Config changed: { scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/2 nav=3/1 orien=2 layout=18}
07-27 16:19:12.065: INFO/ActivityManager(52): Config changed: { scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/2 nav=3/1 orien=1 layout=18}
07-27 16:19:12.305: INFO/System.out(1241): orientation potrait
07-27 16:19:15.485: INFO/ActivityManager(52): Config changed: { scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/1 nav=3/1 orien=2 layout=18}
07-27 16:27:11.465: INFO/ActivityManager(52): Config changed: { scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/2 nav=3/1 orien=2 layout=18}
07-27 16:27:11.665: INFO/ActivityManager(52): Config changed: { scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/2 nav=3/1 orien=1 layout=18}
07-27 16:27:11.885: INFO/System.out(1241): orientation potrait
07-27 16:27:14.035: INFO/ActivityManager(52): Config changed: { scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/1 nav=3/1 orien=2 layout=18}
07-27 16:27:20.401: INFO/ActivityManager(52): Config changed: { scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/2 nav=3/1 orien=2 layout=18}
07-27 16:27:20.675: INFO/ActivityManager(52): Config changed: { scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/2 nav=3/1 orien=1 layout=18}
07-27 16:27:20.865: INFO/System.out(1241): orientation potrait

As you can see there's only notification for when I change the orientation changed from landscape to potrait and not vice versa.

I've tried avd 2.1update1 with api level 7 and avd 2.2 with api level 8. OS is windows xp sp 3 with eclipse 3.6.2.

Is this a bug or there's something wrong with the code?

Community
  • 1
  • 1
user77177928
  • 465
  • 6
  • 15
  • I just take a look, it look OK with your source.it seem strange behavior, could you please send you full source code to my email, very interesting issue :) – NguyenDat Jul 27 '11 at 10:23
  • I've also tried the code. The method `onConfigurationChanged` is not executing in landscape mode. – Mudassir Jul 27 '11 at 10:31
  • @nguyendat I create a new project to test it, and it still don't work. Here's the project code http://s000.tinyupload.com/index.php?file_id=64677725252486745434 – user77177928 Jul 28 '11 at 02:53
  • @Mudassir I thought I'm only one having this problem :) – user77177928 Jul 28 '11 at 02:53
  • android:screenOrientation="portrait" to activity is use for not make change of orientation – Android Jul 28 '11 at 07:08
  • Of course, when the orientation is set to landsacpe one does not expect a configuration change in the orientation. _However_ when the screen is locked on certain phones and the lockscreen rotates, the underlying app rotates as well. The result is a restart of the activity if it cannot detect the orientation change. –  Jun 17 '13 at 13:22

2 Answers2

1

I'm having the same problem but using orientation|keyboardHidden didn't solve it. anyone has an idea?

EDIT: After some trial and error, I found that this works for me:

android:configChanges="keyboardHidden|orientation|screenSize"
yoel
  • 305
  • 4
  • 17
  • For reference/guidance to other users, I am linking to another stack answer which (probably) explains best why this code worked for you: http://stackoverflow.com/a/12226667/269876 – Lo-Tan Mar 25 '13 at 19:50
0

activity-restart-on-rotation-android
how-do-i-detect-screen-rotation and use android:screenOrientation="portrait" to activity is use for not make change of orientation (this can use with the activity tag into the AndroidManifest.xml) like this

<activity android:name="testActivity"
            android:screenOrientation="portrait" android:label="@string/app_name">

(after using this the device will not make sense on change of the orientation)

Community
  • 1
  • 1
Android
  • 8,995
  • 9
  • 67
  • 108
  • I don't want to force screen orientation, I just want to detect if the orientation changed. But thanks for the info. Anyway I already got my answer(see my post below) but I don't know how to close this thread. – user77177928 Jul 28 '11 at 07:31