Question

i want to give the user the option to set the phone's orientation to auto, portrait or landscape.which means for the ENTIRE PHONE's orientation not just my application. every other app must have the landscape orientation too. it was pretty simple to do the first two like this

portrait:

Settings.System.putInt( this.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0);  

auto:

Settings.System.putInt( this.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 1); 

but i can't find a way to set it to landscape. i want to set all apps to landscape not just mine. i see here that it can be done but it doesn't give much information. How to make Android system force LANDSCAPE for all apk?. anyone willing to explain or provide code snippet?

Was it helpful?

Solution

Code below can force your screen to landscape mode. It is similar to set it to other mode.

public class MyService extends Service {
    private boolean mViewAdded = false;

    private WindowManager.LayoutParams mLayoutParams;
    private View mOverlayView;
     private WindowManager mWindowManager;

    // Call this some where in your code
    private void setLandscapeOrientation() {
        mWindowManager = ((WindowManager)getSystemService("window"));
        mOverlayView = new View(this);
        mLayoutParams = new WindowManager.LayoutParams();
        mLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
        mLayoutParams.width = 0;
        mLayoutParams.height = 0;
        mLayoutParams.flags = mLayoutParams.flags |
            WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED |
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
            WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
        mLayoutParams.flags = mLayoutParams.flags &
            ~WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON &
            ~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
        mLayoutParams.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        if (mViewAdded) {
            mWindowManager.updateViewLayout(mOverlayView, mLayoutParams);
        } else {
            mWindowManager.addView(mOverlayView, mLayoutParams);
            mViewAdded = true;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        if (mViewAdded) {
            mWindowManager.removeView(mOverlayView);
            mViewAdded = false;
        }
    }
}

OTHER TIPS

In your AndroidManifest.xml, for each activity put

android:screenOrientation="landscape"

It forces the activity to landscape

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top