0
votes

Welcome all

I have a special need for my app. I must force the app to work only in portrait mode, but i need to know when the user has moved the phone to landscape mode. Why? because i am displaying a opengl view with a texture image, and when the user changues the phone position to landscape mode i must rotate the polygon without reseting the activity. Then i must force portrait mode on manifest, because i dont want that my onCreate method gets called again.

Please, can someone tell me how to achieve this?

I know how to rotate the image, i only need to know when the user has moved the phone to landscape position, but with portrait forced on manifest.

I tryed adding android:screenOrientation="portrait" in the manifest and also adding android:configChanges="keyboardHidden|orientation" to the declaration in the manifest; and then overriding onConfigurationChanged() in my activity. But this doens't works, because portrait mode is forzed, then onConfigurationChanged method is never called......

Thanks

3
Try that: Remove your android:screenOrientation="portrait" from the Manifest. Add the android:configChanges="keyboardHidden|orientation", and override your onConfigurationChanged(). This way your onCreate will not be called twice.Derzu
Derzu, please, can you help me explaining ashok answer? thanksNullPointerException

3 Answers

3
votes

Why not use the SensorManager to monitor when the phone has rotated 90 degrees. Actually this may be helpful also.

0
votes

use this attribute in manifest android:configChanges="orientation" this stops recreating activity then override the onConfigurationChange() method. Give the same layout both in portrait and landscape mode. when orientation change occurs, that method wil be called then change the image as u like

0
votes

Do Not set the screen orientation in the manifest (android:screenOrientation="portrait"). Add the android:configChanges="keyboardHidden|orientation", and override your onConfigurationChanged(). This way your onCreate will not be called twice.

Here a example of your onConfigurationChanged:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig); // tem que ter
    if (getResources().getConfiguration().orientation ==  Configuration.ORIENTATION_LANDSCAPE) {

    } else 
    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {

    }
}