Force an Android Activity to Always Use Landscape Mode

android force lauch activity in landscape mode

Try overriding the onConfigurationChanged method.

    @Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

how to set landscape orientation in particular activity?

To lock your Activity during runtime, you can use

// lock orientation to landscape
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

All you need now is to determine the current orientation, and pass it to keep it locked that way.

First, add these imports:

import android.content.pm.ActivityInfo;
import android.content.res.Configuration;

Then you can add this to your Activitys onCreate():

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 the other orientation configurations.

Android: Force landscape orientation but allow rotation

You just need to set the android:screenOrientation attribute in your AndroidManifest.xml to sensorLandscape



Related Topics



Leave a reply



Submit