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); } }
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.
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.
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
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With