Auto-rotate default screen in app

I have a toggle button in the application. I want to change or manage the default setting, automatically rotate the screen (Settings> Screen> Auto-rotate screen) programmatically. Does anyone know how to do this?

+4
source share
4 answers

Have you tried this in your activity?

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); //This is the default value setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); 

After that, you can use this to turn off automatic orientation:

 public static void setAutoOrientationEnabled(ContentResolver resolver, boolean enabled) { Settings.System.putInt(resolver, Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0); } 

Documentation

+10
source

you can use this:

 android.provider.Settings.System.putInt(getContentResolver(), android.provider.Settings.System.USER ROTATION,user_rotation); 

for rotation policy

 user_rotation 0 -> ROTATION_0 user_rotation 1 -> ROTATION_90 user_rotation 2 -> ROTATION_180 user_rotation 3 -> ROTATION_270 

See http://developer.android.com/reference/android/provider/Settings.System.html#USER_ROTATION for more details.

Also parameter menifiest.xml

 <uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission> 
+7
source

You set the default rotation settings in the manifest file, for example:

 <activity android:name=".MainTabActivity" android:screenOrientation="portrait"> </activity> 

To change the orientation programmatically, you must call Activity.setRequestedOrientation ()

+3
source
 a1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(getApplicationContext(), MainActivity.class)); android.provider.Settings.System.putInt(getContentResolver(), android.provider.Settings.System.USER_ROTATION,0); } }); a2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(getApplicationContext(), MainActivity.class)); android.provider.Settings.System.putInt(getContentResolver(), android.provider.Settings.System.USER_ROTATION,90); } }); 
0
source

Source: https://habr.com/ru/post/1401567/


All Articles