I have an application that I just would like to use in portrait mode, so I have defined android:screenOrientation=\"portrait\" in the manifest XML. This works OK for the HTC
In your androidmanifest.xml file:
<activity android:name="MainActivity" android:configChanges="keyboardHidden|orientation">
or
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
Please note, none of the methods seems to work now!
In Android Studio 1 one simple way is to add
android:screenOrientation="nosensor"
.
This effectively locks the screen orientation.
You need to modify AndroidManifest.xml as Intrications (previously Ashton) mentioned and make sure the activity handles the onConfigurationChanged event as you want it handled. This is how it should look:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
As said, set android:configChanges
of your Activity (in manifest file) to keyboardHidden|orientation
and then:
1) Override onConfigurationChanged()
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
//here you can handle orientation change
}
2) Add this line to your activity's onCreate()
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
It's better than add same line to onConfigurationChanged
, because your app will turn to portrait mode and then back to landscape (it will happen only one time, but it's annoying).
Also you can set android:screenOrientation="nosensor"
for your activity (in manifest). But using this way you're a not able to handle orientation changes at all.
In Visual Studio Xamarin:
using Android.Content.PM;
to you activity namespace list.
[Activity(ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait)]
as an attribute to you class, like that:
[Activity(ScreenOrientation = ScreenOrientation.Portrait)]
public class MainActivity : Activity
{...}
In OnCreate method of your activity use this code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Now your orientation will be set to portrait and will never change.