Search code examples
androidandroid-screen

Android can't detect screen rotation


I have a problem with detecting the screen rotation. I have even tried adding the android:configChanges="orientation|keyboardHidden".

Any help is welcome.

This is my code for the check:

public void onConfigurationChanged(Configuration newConfig) {
  super.onConfigurationChanged(newConfig);
  if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
     Log.v("o", "oo");
  } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
     Toast.makeText(this, "portrait", Toast.LENGTH_LONG).show();
  }
}

Solution

  • 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"

    From https://developer.android.com/guide/topics/resources/runtime-changes.html