0

Below is my Activity where I need to handle orientation change differently, basically whenever onConfigurationChanged() event is fired due to device orientation change, I show a dialog window and ask user whether he wants to update the orientation, based on users action i update to landscape/portrait or keep the same orientation.

public class MyActivity extends Activity {
..
..
private boolean mFlagConfigChangeManual = false;

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        Log.e("onConfigurationChanged", "mFlagConfigChangeManual="+mFlagConfigChangeManual);

        super.onConfigurationChanged(newConfig);

        int orient = -1;
        if (!mFlagConfigChangeManual) {

            if (newConfig.orientation == newConfig.ORIENTATION_LANDSCAPE) {
                mFlagConfigChangeManual = true;
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                orient = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;

            } else if (newConfig.orientation == newConfig.ORIENTATION_PORTRAIT) {
                mFlagConfigChangeManual = true;
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                orient = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;

            }

        } else {
            mFlagConfigChangeManual = false;
            return;
        }

        final int orient2 = orient;
        DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                case DialogInterface.BUTTON_POSITIVE:
                    if (orient2 >= 0) {
                        mFlagConfigChangeManual = true;
                        setRequestedOrientation(orient2);
                    }
                    break;

                case DialogInterface.BUTTON_NEGATIVE:
                    break;
                }
            }
        };

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Would you like to update orientation?")
                .setPositiveButton("Yes", dialogClickListener)
                .setNegativeButton("No", dialogClickListener).show();

}

I have made the required changes in manifest file but I face two issues, first time orientation confirmation dialog is displayed it works but when I re-orient again, it does not do anything, basically onConfigurationChanged() is not called another time. Another issue I have noticed is;

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); method orients the view upside down when landscape mode. Can this be done better way?

duckduckgo
  • 1,280
  • 1
  • 18
  • 32

1 Answers1

0

For the first question, the onConfigurationChanged method is not called after the first change, because when u manually force an orientation the listening that triggers the onConfigurationChanged method is disabled, that's to keep the new orientation, for example, if the device is in portrait and you to force landscape what will occur if the listening is triggered again? The value of newConfig.orientation portrait will be again 'cause this will be the actual orientation of the device.

To solve this first problem is not so simple, because we need to manipulate the orientation through OrientationEventListener and check the degree of rotation device.

see here! manually-confirm-for-orientation-change

For the second problem u just need to change the setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

by this setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);

Edited:

to re-enable the sensor u need to call the setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_SENSOR);

Community
  • 1
  • 1
Anderson K
  • 5,445
  • 5
  • 35
  • 50
  • but when i make manual call to setRequestedOrientation() in first block of code onConfigurationChanged event is fired which i am filtering using mFlagConfigChangeManual flag, second time same thing did not worked. – duckduckgo Mar 03 '15 at 13:38
  • @AbhishekK , to re-enable the sensor u need to call the setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_SENSOR); – Anderson K Mar 03 '15 at 13:43
  • yes this works perfectly .. thank you. you may include this detail to your answer. – duckduckgo Mar 03 '15 at 13:45