Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use onConfigurationChanged() and newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE in android 2.3.3

Tags:

android

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);         }      } 
like image 814
Rajesh Avatar asked Mar 05 '12 12:03

Rajesh


People also ask

How to use onConfigurationChanged in Android?

The onConfigurationChanged() method is passed a Configuration object that specifies the new device configuration. By reading fields in the Configuration object, you can determine the new configuration and make appropriate changes by updating the resources used in your interface.

How do I change the orientation of my screen on Android?

You can do this by setting the android:configChanges flag on your Activity in AndroidManifest. xml as shown below: This flag signals to the Android platform that you are going to manually handle orientation, screenSize and keyboard appearance/disappearance changes for this Activity.


2 Answers

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

like image 181
Ajay Avatar answered Oct 16 '22 20:10

Ajay


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).

like image 42
Lukas Hanacek Avatar answered Oct 16 '22 20:10

Lukas Hanacek