Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect when the device switch from portrait to landscape mode? [duplicate]

I have an app which shows fullscreen bitmaps in an activity. In order to provide fast loading time, I load them in the memory. But when the screen changes orientation, I would like to clear the cache in order to fill it again with bitmaps that fit inside the new dimensions. The only problem is that in order to do this, I need to detect when an orientation change occurs. Do anyone know how to detect this?

like image 252
Quentin Golsteyn Avatar asked Aug 19 '13 19:08

Quentin Golsteyn


People also ask

How do you know when your screen rotation is fluttering?

How to Detect Orientation Change in Layout In Flutter ?? In order to determine the Orientation of the screen, we can use the OrientationBuilder Widget. The OrientationBuilder will determine the current Orientation and rebuild when the Orientation changes.

Which methods are called when the screen changes orientation from portrait to landscape in Android?

In this method, when you switch from Portrait to Landscape, a method is called, onConfigurationChanged method.

How do you know if its landscape or portrait in flutter?

To determine the app's current Orientation , use the OrientationBuilder widget. The OrientationBuilder calculates the current Orientation by comparing the width and height available to the parent widget, and rebuilds when the size of the parent changes.


4 Answers

See the official documentation http://developer.android.com/guide/topics/resources/runtime-changes.html

Changing it will actually create a new view and onCreate will be called again.

Furthermore you can check it via

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
}
like image 131
noone Avatar answered Oct 05 '22 08:10

noone


You can check the onSavedInstanceState from your onCreate method, if it is not null means this is configuration change.

like image 37
Eugene Avatar answered Oct 05 '22 08:10

Eugene


Another approach is using OrientationEventListener.

It can be used like this:

 OrientationEventListener mOrientationEventListener = new OrientationEventListener(
            this, SensorManager.SENSOR_DELAY_NORMAL) {

        @Override
        public void onOrientationChanged(int orientation) {
            //checking if device was rotated
            if (orientationPortrait != isPortrait(orientation)) {
                orientationPortrait = !orientationPortrait;
                Log.d(TAG, "Device was rotated!");
            }
        }
    };

To check orientation:

private boolean isPortrait(int orientation) {
    return (orientation >= (360 - 90) && orientation <= 360) || (orientation >= 0 && orientation <= 90);
}

And don't forget to enable and disable listener:

if (mOrientationEventListener != null) {
        mOrientationEventListener.enable();
    }

if (mOrientationEventListener != null) {
        mOrientationEventListener.disable();
    }
like image 32
Bogdan Ustyak Avatar answered Oct 05 '22 09:10

Bogdan Ustyak


Usually Orientation change calls OnCreate() unless you have done something to make it do otherwise.

You can put the logic there.

like image 32
sealz Avatar answered Oct 05 '22 09:10

sealz