Reputation: 51
How I can control programmatically screen orientation of overall device and change it? Here links to some apps that can do that:
http://dandroidtabletpc.com/download-total-screen-control-1-9-3-full-android-apk.html/
http://ru.androidzoom.com/android_applications/tools/ultimate-rotation-control_bznhn.html
maybe somebody knows open source projects like this?
Upvotes: 4
Views: 15750
Reputation: 34775
Firstly, in android if you are not setting any orientation either through code or xml file then your app by default has orientation.
And if your requirement is that inspite of phone is in portrait/landscape mode and you require an single oreintation then you can lock it through code as follow in oncreate method::
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); //or
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Upvotes: 0
Reputation: 42417
Here are two approaches I know of:
If you want the orientation you set to override apps' own orientation preferences, follow the instructions in How can I globally force screen orientation in Android?.
If you don't want the orientation you set to override apps' own orientation preferences, first ensure the phone's automatic rotation is turned off:
Settings.System.putInt(
this.contentResolver,
Settings.System.ACCELEROMETER_ROTATION,
0 //0 means off, 1 means on
);
Then set the phone's user orientation:
Settings.System.putInt(
getContentResolver(),
Settings.System.USER_ROTATION,
Surface.ROTATION_0 //Use any of the Surface.ROTATION_ constants
);
Don't forget to check the return values of putInt
to ensure the change was successful.
Also, I think you need the following permission:
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
Upvotes: 5
Reputation: 2005
Using below code you can get current orientation of activity.
int orientation = getResources().getConfiguration().orientation;
if(orientation == Configuration.ORIENTATION_LANDSCAPE)
{
Log.v(TAG,"Configuration.ORIENTATION_LANDSCAPE");
}
else if(orientation == Configuration.ORIENTATION_PORTRAIT)
{
Log.v(TAG, "Configuration.ORIENTATION_PORTRAIT");
}
else if(orientation == Configuration.ORIENTATION_SQUARE)
{
Log.v(TAG, "Configuration.ORIENTATION_SQUARE");
}
else if(orientation == Configuration.ORIENTATION_UNDEFINED)
{
Log.v(TAG, "Configuration.ORIENTATION_UNDEFINED");
}
Then using below code you can set activity orientation to either portrait or landscape.
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Upvotes: -1
Reputation: 4216
@Override
public void onCreate(Bundle savedInstanceState) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
or
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Upvotes: 13