overbet13
overbet13

Reputation: 1684

Detect orientation change, when only portrait is allowed on Android

I have to solve the following: I have an Activity which's android:screenOrientation="portrait". Even though, when the device is rotated to landscape while this Activity is visible, I have to start another one, and, when the device is rotated back to portrait, I have to finish() the activity in landscape. I tried to perform this with a BroadcastReceiver, but this special activity doesn't receive any broadcasts because of the android:screenOrientation="portrait". Any help is well appreciated.

Thanks.

Upvotes: 5

Views: 8034

Answers (7)

Gal Rom
Gal Rom

Reputation: 6471

after a little struggle the best way i found was using OrientationEventListener like so:

private void addOrientationListener()
    {
        OrientationEventListener listener=new OrientationEventListener(getActivity(), SensorManager.SENSOR_DELAY_UI)
        {
            public void onOrientationChanged(int orientation) {

                if( (orientation>=230 && orientation<=290) || (orientation>=70 && orientation<=90))
                {
                    isLandscape=true;
                }
                else if(orientation==-1){
                    //KEEP THE CURRENT SATTE
                }
                else
                {
                    isLandscape=false;
                }
            }
        };
        if(listener.canDetectOrientation())
            listener.enable();
    }

you should call disable() on OnPause()

Upvotes: 1

Wile E. Genius
Wile E. Genius

Reputation: 185

Philipp's solution in Get phone orientation but fix screen orientation to portrait work's perfectly for me:

You can use the SensorManager class to get the orientation of the Android device, even when the automatic orientation switching is disabled in the Manifest by android:screenOrientation="portrait"

See this code (by Philipp, see link above):

SensorManager sensorManager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
    sensorManager.registerListener(new SensorEventListener() {
        int orientation=-1;;

        @Override
        public void onSensorChanged(SensorEvent event) {
            if (event.values[1]<6.5 && event.values[1]>-6.5) {
                if (orientation!=1) {
                    Log.d("Sensor", "Landscape");
                }
                orientation=1;
            } else {
                if (orientation!=0) {
                    Log.d("Sensor", "Portrait");
                }
                orientation=0;
            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
            // TODO Auto-generated method stub

        }
    }, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);

Upvotes: 8

Gan
Gan

Reputation: 1399

You can do it in a couple of ways, one is listening to broadcast messages as discussed in other posts and the other option is as follows.

Set the android:configChanges="orientation" in the manifest for the activity which will give you control over the orientation change. By doing so you can override the onConfigurationChanged() method and start your new activity activity.

Upvotes: 0

Chandrashekhar
Chandrashekhar

Reputation: 498

When android:screenOrientation="portrait" or "landscape" are set in the menifest file no listeners are fired still if u want to do it try handling the portrait only mode in ur onConfigurationChanged() programatically and here u will also be able to start the activity again.

Upvotes: 5

ZZZ
ZZZ

Reputation: 788

You can always find out your orientation using sensors;

public static final int PORTRAIT        = 0;
public static final int PORTRAIT_REV    = 2;
public static final int LANDSCAPE       = 1;
public static final int LANDSCAPE_REV   = 3;

private final SensorEventListener mListener = new SensorEventListener() {

   public void onSensorChanged(SensorEvent event) {


            azimuth = event.values[0];
            pitch   = event.values[1];
            roll    = event.values[2];

            {
                if (pitch < -45 && pitch > -135) {
                    orient = PORTRAIT;
                } else if (pitch > 45 && pitch < 135) {
                    orient = PORTRAIT_REV;
                } else if (roll > 45) {
                    orient = LANDSCAPE;
                } else if (roll < -45) {
                    orient = LANDSCAPE_REV;
                }

            }

           // if(orient!=lastOrient && !orientChanging)
           // {
           //   orientChangeStart = System.currentTimeMillis();
           //   orientChanging = true;
           // }
}

Upvotes: 1

SD_Guru
SD_Guru

Reputation: 339

The class that you need to use is the OrientationEventListener, assuming that you can detect rotation. If you force the screen into a specific orientation using android:screenOrientation="portrait", these events won't be fired.

Instead, I'd recommend making two layout.xml files and use configuration qualifiers to determine which one should be displayed. (http://developer.android.com/guide/practices/screens_support.html)

res/layout-land/my_layout.xml
res/layout-port/my_layout.xml

Then use the OrientationEventListener to detect when the screen is rotated. When the screen is rotated from landscape to portrait, you can finish(); or call whichever functions you'd like.

Best of luck!

Upvotes: 0

Wait what
Wait what

Reputation: 90

Have you used Orientation Listener? I'm not sure if that's exactly what you need, but I'm gathering that you want to find out when the orientation is changing back and forth:

http://developer.android.com/reference/android/view/OrientationListener.html

Upvotes: 0

Related Questions