I just installed the app Rotation Locker a moment ago. It is a simple app but it work so great. It can force all other apps run in landscape or portrait mode. I am struggling to figure out how it can do this. Can anybody tell me, please?
Asked
Active
Viewed 419 times
2 Answers
1
Piecing together data from other posts I was able how to work this out for an app I was writing that needs to do exactly this. (Force Dock Rotation if you're interested).
My solution involves creating a window and inserting it at the top of the view hierarchy so that you can get first shout of the orientation before another app tries to set it...
orientationChanger = new LinearLayout(this);
// Using TYPE_SYSTEM_OVERLAY is crucial to make your window appear on top
// You'll need the permission android.permission.SYSTEM_ALERT_WINDOW
WindowManager.LayoutParams orientationLayout = new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, 0, PixelFormat.RGBA_8888);
// Use whatever constant you need for your desired rotation
orientationLayout.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR;
WindowManager wm = (WindowManager) this.getSystemService(Service.WINDOW_SERVICE);
wm.addView(orientationChanger, orientationLayout);
orientationChanger.setVisibility(View.VISIBLE);
Credit to this answer for the inspiration: How can I globally force screen orientation in Android?

Community
- 1
- 1

chrispaton
- 361
- 2
- 3
-
The code that you have shared is not working for me. I want to make default orientation of device to be portrait on some condition. When i received that event i had written your code , but its not changing orientation to Landscape. – Roll no1 Jun 09 '14 at 13:31
0
Probably you can just hold the SEARCH button or some other button.
This is the way it works on HTC Desire

tomi
- 525
- 9
- 20
-
Thanks for your reply but i think you dont understand what i asked. What i really want to know is that how can i do to create a similar app like that. – Nguyen Minh Binh Apr 28 '12 at 17:06