Adley Adley - 2 years ago 112
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

public void onConfigurationChanged(Configuration 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();



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

Answer Source

Try this:


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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download