Adley Adley - 6 months ago 18
Android Question

Check android when change orientation screen

I've an activity that shows an image, but when I change orientation, the image stretch, so I want to reduce the width of the layout.

The problem was, I already researched about it and tried to do this:

My Actitivy

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

// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}


AndroidManifest

<activity
android:configChanges="orientation|keyboardHidden"
</activity>


But doesn't work. I try to debug, but never reach a breakpoint inside of the onConfigurationChage method.

Answer

Try this:

android:configChanges="orientation|screenSize"

Because 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