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?

6
  • 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, 2011 at 10:23
  • I've also tried the code. The method onConfigurationChanged is not executing in landscape mode.
    – Mudassir
    Jul 27, 2011 at 10:31
  • @nguyendat I create a new project to test it, and it still don't work. Here's the project code s000.tinyupload.com/index.php?file_id=64677725252486745434 Jul 28, 2011 at 2:53
  • @Mudassir I thought I'm only one having this problem :) Jul 28, 2011 at 2:53
  • android:screenOrientation="portrait" to activity is use for not make change of orientation
    – Android
    Jul 28, 2011 at 7:08

2 Answers 2

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"
1
  • For reference/guidance to other users, I am linking to another stack answer which (probably) explains best why this code worked for you: stackoverflow.com/a/12226667/269876
    – Lo-Tan
    Mar 25, 2013 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)

1
  • 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. Jul 28, 2011 at 7:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.