How to set the orientation of the landscape in particular?
I am showing a graph in my activity using a graph. Now I need to set the orientation of the activity according to the rotation of the user. This means that if the user is going to move horizontally, the activity should set his orientation as "landscape" and the same for vertical, ie. "Portrait". It shouldn't be affected by turning mobile settings. I just want it just for my graphics work.
here is a short window that you can easily understand.
source to share
To block your activity at runtime you can use
// lock orientation to landscape
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Now you need to determine the current orientation and pass it in so that it is locked that way.
First add the following imports:
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
Then you can add this to your onCreate () actions:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// lock the current device orientation
int currentOrientation = this.getResources().getConfiguration().orientation;
if (currentOrientation == Configuration.ORIENTATION_PORTRAIT){
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
else{
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
// ... rest of your code
}
See ActivityInfo documentation for other orientation configurations.
source to share
In your manifest file add the following code, which action needs to be shown as landscape,
<activity android:name=".activity name of yours"
android:configChanges="orientation|screenSize|keyboardHidden">
</activity>
or
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
source to share