Search code examples
androidandroid-orientationdevice-orientation

How to restrict tablet screen orientation


I want to restrict my tablet to launch at specific screen orientation like landscape? I'm taking here about the whole tablet not a specific app, and for sure I'm not taking about locking off the Auto Rotate in the tablet after it launches, I'm taking about restricting the tablet to launch at landscape/protrait.


Solution

  • found the answer, an app that will run just one time.

    create an app and add a permission to it's manifest to write_settings & listen-to-boot :

    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    

    then make a receiver class which extends BroadcastReceivertype, with the fowlloing permissions:

        <receiver android:enabled="true" android:exported="true" 
            android:name="com.example.BootCompletedReceiver"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                    <action android:name="android.intent.action.QUICKBOOT_POWERON" />
                    <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>
    

    then in the onReceive() method of the receiver class:

    ContentResolver contentResolver = context.getContentResolver();
            //To handle device rotation 
            Settings.System.putInt(contentResolver, Settings.System.ACCELEROMETER_ROTATION, 0);
            Settings.System.putInt(contentResolver, Settings.System.USER_ROTATION, Surface.ROTATION_90); // 0 for default, then 90, 180, 270
    

    Then run this app just one time to write that permissions to the system, and then you don't need it again.