0

Possible Duplicate:
How to disable orientation change in Android?

I want to disable orientation change after opening the canvas in any orientation(portrait or landscape) , but I am having problem when open the canvas in the landscape mode and change the device orientation, the orientation is not disabled.

here is the code .

public void onConfigurationChanged(Configuration newConfig) {

  super.onConfigurationChanged(newConfig);
  this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

}
Community
  • 1
  • 1
Vikas Panwar
  • 399
  • 1
  • 3
  • 14

3 Answers3

6

try this in your code...

if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
    {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }
    else
    {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

get the orientation in oncreate and set that orientation for that activity

Thirupathig
  • 166
  • 9
3

Add a fixed orientation for your activity from manifest using this code :(put this code inside activity tag )

android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden"

by this your activity will be displayed in portrait mode only

Shruti
  • 1
  • 13
  • 55
  • 95
  • Thanks Shruti, but i don't want to open canvas only in one orientation it should be open in both the mode portrait and landscape initially but after opening the canvas, the orientation changes should be disabled. the above code works correctly if i opens the canvas in portrait mode. – Vikas Panwar Nov 01 '12 at 05:09
  • ok so may be u need to use `android:configChanges="orientation|keyboardHidden"` this line only.. – Shruti Nov 01 '12 at 05:27
1

use:

<activity android:name="YourActivity" android:configChanges="orientation|keyboardHidden">
Trần Sĩ Long
  • 457
  • 3
  • 15