Search code examples
androidandroid-layoutandroid-orientation

Android detect device orientation when requestedOrientation is specified


I'm building an app for video streaming. It has a layout Youtube-style, with a portrait layout showing the video on top and a list of other content on the bottom. The app can show the video fullscreen on landscape either by rotating the device, or by clicking a button.

Now, if I rotate the phone to landscape and then I rotate it back to portrait, the layout is going back to portrait as expected. If instead I click the button to force the portrait mode, rotate the device in landscape to see the video, and then I rotate it back to portrait, the layout remains stuck to landscape.

My activity in the manifest:

    <activity
        android:name="MyActivity"
        android:configChanges="keyboardHidden|orientation|screenSize|smallestScreenSize|screenLayout"
        android:label="@string/app_name"
        android:launchMode="singleTask" />

My button click:

mChangeOrientButton.setOnClickListener((OnClickListener)(new OnClickListener() {
    public final void onClick(View it) {
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } else if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
        }
    }
}));

My onConfigurationChanged method (I noticed that if I click the button and then rotate the phone, this method is not called anymore, as long as the requestedOrientation remains set in something different than UNSPECIFIED):

@Override
public void onConfigurationChanged(Configuration newConfig)
{
    super.onConfigurationChanged(newConfig);
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
    {
        // Set the layout for landscape mode
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        // Set the layout for portrait mode        
    }
}

I tried to reset the requestedOrientation to UNSPECIFIED like this:

@Override
public void onConfigurationChanged(Configuration newConfig)
{
    super.onConfigurationChanged(newConfig);
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
    {
        // Set the layout for landscape mode
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        // Set the layout for portrait mode        
    }
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}

but it doesn't work, because the orientation is forced from portrait to landscape and then set again to unspecified before the user has a chance to rotate it to landscape, so it immediately changes again to portrait and it seems that it's doing nothing.


Solution

  • Ok, I found a solution looking at this other answer. It was not working for me, so I modified it slightly:

    MainActivity

    import android.content.Context;
    import android.hardware.Sensor;
    import android.hardware.SensorEvent;
    import android.hardware.SensorEventListener;
    import android.hardware.SensorManager;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.view.Surface;
    import android.widget.ImageView;
    
    public class MainActivity extends AppCompatActivity {
    
        private SensorManager mSensorManager;
        private Sensor mOrientation;
    
        private boolean mIsForcingOrientation = false;
    
        float value_0 = -10000;
        float value_1 = -10000;
    
        private SensorEventListener mOrientationSensorListener = new SensorEventListener() {
            int orientation = -1;
    
            @Override
            public void onSensorChanged(SensorEvent event) {
                int value ;
                if(value_0 == event.values[0] && value_1==event.values[1]){
                    return;
                }
                if (value_0 < 0 && event.values[0] > 0){
                    // Setting rotation to 270°: Landscape reverse
                    value = Surface.ROTATION_270;                
                } else if (value_0 > 0 && event.values[0] < 0){
                    // Setting rotation to 90°: Landscape
                    value = Surface.ROTATION_90;                
                } else if (value_1 < 0 && event.values[1] > 0){
                    // Setting rotation to 180°: Portrait reverse
                    value = Surface.ROTATION_180;                
                } else if (value_1 > 0 && event.values[1] < 0){
                    // Setting rotation to 0°: Portrait
                    value = Surface.ROTATION_0                
                } else {
                    value = orientation;
                }
    
                if (orientation != value) {
                    if((requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT && (value == Surface.ROTATION_90 || value == Surface.ROTATION_270) ) ||
                    (requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE && (value == Surface.ROTATION_0 || value == Surface.ROTATION_180) )     ){
                        isForcingOrientation = false;
                        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
                    }
                }
    
                value_0=event.values[0];
                value_1=event.values[1];
            }
    
            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {
    
            }
        };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            // Get sensor manager
            mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
            // Get the default sensor of specified type
            mOrientation = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    
            mChangeOrientButton.setOnClickListener((OnClickListener)(new OnClickListener() {
                public final void onClick(View it) {
                    mIsForcingOrientation = true;
                    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                    } else if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
                        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                    } else {
                        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
                    }
                }
            }));
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            if (mOrientation != null) {
                mSensorManager.registerListener(mOrientationSensorListener, mOrientation,
                        SensorManager.SENSOR_DELAY_GAME);
            }
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            if (mOrientation != null) {
                mSensorManager.unregisterListener(mOrientationSensorListener);
            }
        }
    }