2

I'm trying to get the current orientation of an activity and lock it like this but it just goes to portrait even when opened in landscape mode.

//getRequestedOrientation();
setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_LOCKED);

Thanks

MosesA
  • 925
  • 5
  • 27
  • 53

2 Answers2

0

You have screen orientation locked, it will show your app in portrait mode, you should use screen orientation sensor.

setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_SENSOR);   

Edit.

int str = 0;
    try {
            str = Settings.System.getInt(contexto.getContentResolver(),Settings.System.ACCELEROMETER_ROTATION);
        } catch (SettingNotFoundException e) {          
                e.printStackTrace();
        } 
    if(str!=0) 
    {
        setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_SENSOR);   
    }

Also check this link, I think it can help you.

Community
  • 1
  • 1
  • I want it to sense the orientation and stay on the orientation even when titled. – MosesA Jul 25 '14 at 11:40
  • I'm not sure what the first part of the code is doing with the Settings. I just tried it, it still changes orientation to portrait even when I clicked the app on landscape. – MosesA Jul 25 '14 at 11:53
0

This seems to do the trick:

if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
MosesA
  • 925
  • 5
  • 27
  • 53