Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting orientation like in camera app

I am developing application that is somehow similar to camera, I wish that the app will always be in landscape mode witch I achieve with:

android:screenOrientation="portrait"

But I need to know when user rotates the phone to landscape mode so I can rotate the icons 90 degrees.

I have tried:

   if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) 
    {
        Toast.makeText(getApplicationContext(), "ORIENTATION_LANDSCAPE", Toast.LENGTH_SHORT).show();

    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){

        Toast.makeText(getApplicationContext(), "ORIENTATION_PORTRAIT", Toast.LENGTH_SHORT).show();
    }

But it only works when I add:

android:configChanges="keyboardHidden|orientation|screenSize"

And its also change my orientation automatically.

So how can I keep my app always in Landscape mode and only detect when the phone is in Portrait without that phone automatically change anything? Just like in camera app

like image 693
Dim Avatar asked Mar 12 '26 08:03

Dim


1 Answers

Using android:configChanges="keyboardHidden|orientation|screenSize" is a good idea.

Now try setting your orientation manually:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
like image 177
Ilya Gazman Avatar answered Mar 13 '26 22:03

Ilya Gazman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!