39

I am using onConfigurationChanged(). In that, when I am changing from LandScape to Portrait, it is calling if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) and shifting to Portrait from LandScape. But when I am changing from Portrait to Land-Scape, it is not changing to LandScape because it is calling if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) so, it is not changing from LandScape to Portrait. Please help.

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

        //int orientation = this.getResources().getConfiguration().orientation;

        if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            Log.d("Entered to change as Portrait ","PPPPPPPPPPPPPPPPP");
            setContentView(R.layout.settings);
        } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Log.d("Entered to change as LandScape ","LLLLLLLLLLLLLLLLLLLL");
            setContentView(R.layout.settings);
        }

    }
Yury
  • 20,618
  • 7
  • 58
  • 86
Rajesh
  • 449
  • 1
  • 6
  • 13

6 Answers6

52

Just write the below code into onConfigurationChanged method and test

if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){

    Log.e("On Config Change","LANDSCAPE");
}else{

    Log.e("On Config Change","PORTRAIT");
}

and write the android:configChanges="keyboardHidden|orientation" into your manifiest file like this

<activity android:name="TestActivity"
           android:configChanges="keyboardHidden|orientation">

it's working at my side, i hope it helps you.

If you're on tablet also add |screenSize to android:configChanges

Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
Ajay
  • 4,850
  • 2
  • 32
  • 44
21

As noted in the Handling Runtime Changes section of the docs, you should also be aware of:

Caution: Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the "screenSize" value in addition to the "orientation" value. That is, you must decalare android:configChanges="orientation|screenSize". However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Lukas Hanacek
  • 1,364
  • 14
  • 25
4

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.

Deva
  • 3,919
  • 1
  • 27
  • 38
2

In addition to above answers: If you override the onConfigurationChanged method in combination with the updating of the manifest file you are saying you are handling the configurationchanges yourself. Therefore adding:

setContentView(R.layout.layout.xml);

In your onConfigurationChanged method - where layout is your layout file - will fix the problem.

Sanjay Joshi
  • 2,036
  • 6
  • 33
  • 48
2

Write below code in your activity file.

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

    if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
     Log.e("On Config Change","LANDSCAPE");
    }
    else{
     Log.e("On Config Change","PORTRAIT");
    }

}

Also write below code in your manifest file as follows:

<activity
        android:name=".activities.TestActivity"
        android:configChanges="orientation|screenSize|keyboardHidden"
/>
0

Write this line in the android Manifest.xml file:

<activity
  android:name="MyActivity"
  android:configChanges="orientation|keyboardHidden" />
Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53
Pradeep Sodhi
  • 2,135
  • 1
  • 19
  • 19