4

I'm using:

setRequestedOrientation(getResources().getConfiguration().orientation);

and later on:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

it prevents the orientation to change until a task is finished, but it only works on portrait, when app is on landscape it doesn't stop orientation to change.

Any suggestions?
Oim~

Artemio Ramirez
  • 1,116
  • 1
  • 10
  • 23
  • if you want to keep the orientation fixed to potrait mode then use android:orientation=potrait in each activity in the manifest – therealprashant Oct 27 '14 at 05:03
  • This can help you to disable rotating device programmatically: http://stackoverflow.com/a/3614089/3864698 – QArea Oct 27 '14 at 11:07
  • @therealprashant I'm not trying to fix the orientation for the entire application run but only while a crucial task is running. – Artemio Ramirez Oct 27 '14 at 16:49
  • @QArea that didn't work either but it led me to find the actual answer: getWindowManager().getDefaultDisplay().getRotation() lets you know in wich of all 4 orientation the devise is in, and then you can just fix it using the setRequestedOrientation() – Artemio Ramirez Oct 27 '14 at 16:51

2 Answers2

5

I got it to work properly in all cases now.

To fix the screen:

if (getWindowManager().getDefaultDisplay().getRotation()== Surface.ROTATION_0)
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
if (getWindowManager().getDefaultDisplay().getRotation()== Surface.ROTATION_90)
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
if (getWindowManager().getDefaultDisplay().getRotation()== Surface.ROTATION_270)
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);

and then to allow rotations again:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

Oim~

Artemio Ramirez
  • 1,116
  • 1
  • 10
  • 23
  • Do you test it on tablets? Tablets have different orientation. Rotation_0 means Landscape. – Oximer Dec 07 '15 at 19:01
  • Sorry for the late answer, I've tested the application on a tablet only once, and as far as I remember it was fine, but I wasn't actively trying to test this. Sorry – Artemio Ramirez Jan 28 '16 at 16:03
0

In manifest file with your activity try this

    android:screenOrientation="sensorLandscape"
    android:windowSoftInputMode="stateAlwaysHidden" 

Inside application tag in manifest file try this

    android:hardwareAccelerated="true"
    android:largeHeap="true"
DJhon
  • 1,548
  • 3
  • 22
  • 39
  • Exact same behaviour, it works fine while in portrait, but does nothing while on landscape. – Artemio Ramirez Oct 27 '14 at 04:50
  • Thanks for trying, but keeps being the exact same, it works properly while on portrait, and nothing while on landscape. I kept the previous lines and added the second ones. – Artemio Ramirez Oct 27 '14 at 06:28