3

I have prepared one application.My application will be supports both landscape and portrait. When i change the orientation from portrait to landscape it is working fine,But when i change landscape to portrait it is not working.my code is,

  public void onConfigurationChanged(Configuration newConfig) {

        super.onConfigurationChanged(newConfig);
        System.out.println("orientation---"+getResources().getConfiguration().orientation);
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            setContentView(R.layout.main);
            System.out.println("landscape-----");
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            setContentView(R.layout.main);
            System.out.println("portrait-----");
        }

    }

Every time time it taking only landscape mode.I made mistake some where. please help.

System.out.println("orientation---"+getResources().getConfiguration().orientation);

The above SOP is printing 2 means landscape.Please help me.(i am using 2.3.3)

1
  • The same code once check in 2.3.3. Mar 5, 2012 at 14:21

3 Answers 3

2

You might be hitting this bug "Orientation does not change from landscape to portrait on Emulator on 2.3"

More than 45 people are facing the same issue with the androidd 2.3 emulator

http://code.google.com/p/android/issues/detail?id=13189

Host OS: Windows 7 SDK tools version : revision 8 Eclipse version: 3.5 ADT plug-in version: 8.0.1 Platform targeted by your project: 2.3 Version of the platform running in the emulator:2.3

STEPS TO REPRODUCE: 1. Activity 1 (no screen orientation) - Activity 2 (screen orientation = landscape)

  1. Going from Activity 1 to 2 works fine. Pressing back changes the orientation of Activity 1 to landscape

EXPECTED RESULTS: Activity 1 should be in portrait mode

OBSERVED RESULTS: Behavior is observed only in gingerbread, works as expected on froyo.

6
  • ya i have added "configChanges" attribute to manifest file like this "android:configChanges="orientation|keyboardHidden"" Mar 5, 2012 at 12:13
  • Please provide us more information, if you have android:configChanges="orientation|keyboardHidden it should work fine. Add more code.
    – Soham
    Mar 5, 2012 at 13:55
  • @Soham: Poster is telling that onConfigurationChanged is called. Please tell us how it is possible without specifying android:configChanges="orientation" in android manifest.
    – AndroDev
    Mar 5, 2012 at 14:10
  • Thank u so much Soham...It is working fine for all version expect 2.3.3. I have checked all versions.Please check once in 2.3.3.Please help me Mar 5, 2012 at 14:17
  • Ahaa, look at this bug filed on AOSP - "Orientation does not change from landscape to portrait on Emulator on 2.3" About 45 ppl reported it, it seems to be a common problem :) code.google.com/p/android/issues/detail?id=13189 Can you try running it on a real device with 2.3.3?
    – Soham
    Mar 5, 2012 at 15:11
2

The issue is with the android:configChanges="orientation|keyboardHidden" .

if you remove |keyboardHidden from the androidmanifest.xml, then the onConfigurationChanged is only fired when you rotate from landscape to portrait, not when you go from portrait to landscape (at least in the default emulator).

Hope this helps.

EDIT: DID some sample program to test it and works fine for me.

CheckOrientationActivity.java

import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class CheckOrientationActivity extends Activity {


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_layout);
        Log.i("DayElevenActivity", "onCreate Start");
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Log.i("DayElevenActivity", "onConfigurationChanged");
        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Log.i("ORIENTATION_LANDSCAPE", "ORIENTATION_LANDSCAPE");
            Toast.makeText(getBaseContext(), "ORIENTATION_LANDSCAPE", Toast.LENGTH_SHORT).show();
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            Log.i("ORIENTATION_PORTRAIT", "ORIENTATION_PORTRAIT");
            Toast.makeText(getBaseContext(), "ORIENTATION_PORTRAIT", Toast.LENGTH_SHORT).show();
        }
    }

}

In AndroidManifest.xml

<activity
            android:label="@string/app_name"   
            android:configChanges="**orientation|keyboardHidden**"    
            android:name=".CheckOrientationActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

Corresponding Logs when i run and change the orientation

03-05 18:54:31.644: I/CheckOrientationActivity (20314): onCreate Start
03-05 18:54:35.014: I/CheckOrientationActivity (20314): onConfigurationChanged
03-05 18:54:35.014: I/ORIENTATION_LANDSCAPE(20314): ORIENTATION_LANDSCAPE
03-05 18:54:41.984: I/CheckOrientationActivity (20314): onConfigurationChanged
03-05 18:54:41.984: I/ORIENTATION_PORTRAIT(20314): ORIENTATION_PORTRAIT
5
  • I remove "|keyboardHidden " from manifest file and run the application when i change the orientation from landscape to portrait or portrait application is automatically closing. Mar 5, 2012 at 12:54
  • Edited my answer. Please check the same. Works fine for me.
    – Deva
    Mar 5, 2012 at 13:35
  • Thank u so much Deva...It is working fine for all version expect 2.3.3. I have checked all versions.Please check once in 2.3.3.Please help me Mar 5, 2012 at 14:15
  • did u check with 2.3.3 emulator? Mar 6, 2012 at 5:55
  • yes the issue is there on 2.3.3 emulator. Don't have a real 2.3.3 device to validate it on a real device.
    – Deva
    Mar 6, 2012 at 6:53
1

There's multiple reasons why this could happen, hard to tell without seing more code.

Here's some pointers:

  • you haven't forced the layout to some particular orientation via manifest file or programatically (something like <activity android:screenOrientation="portrait"> in your manifest)

  • by default the Activity is restarted when configuration changes happend. If you want to NOT have your activity restarted when the orientation changes, you need to use

    android:configChanges="orientation"

on your activity's configuration in the manifest.

  • the above point will in fact make Android call your activity's onConfigurationChanged(Configuration) method when the orientation changes

==update==

Also, if you have the same layout (like "main") declared in layout-land and layout-port you only need to specify that you want to use that layout in the beginning:

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

and that's it. THERE IS NO NEED to also implement the

public void onConfigurationChanged(Configuration newConfig) {
  //...
}

method as well. For the sake of chosing the correct layout based on orientation all you have to do is declare the same layout in those two folders.

(Also, there is NO NEED to add the android:configChanges="orientation" to your manifest, apparently it's on by default). I've tried all this just now, works ok

7
  • Well,ok, but I see that you set the exact same layout for either of the 2 orientations. Try making two different layouts for vertical and horizontal and switch the appropriate one when the orientation changes. This will rearrange the widgets in your view. Mar 5, 2012 at 12:22
  • i have prepared two layouts for landscape and portrait.All landscape layouts placed in layout-land folder in res and all portrait layouts placed in layout-port folder in res Mar 5, 2012 at 12:26
  • what is the problem is "getResources().getConfiguration().orientation" is returns always 2(means landscape) Mar 5, 2012 at 12:29
  • try calling newConfig.orientation instead of getResources().getConfiguration().orientation Mar 5, 2012 at 12:32
  • Please once see my code.I have used both thing in different places. Mar 5, 2012 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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