I'm trying to force the "portrait" mode for my application because my application is absolutely not designed for the "landscape" mode.
After reading some forums, I added these lines in my manifest file:
<application
android:debuggable="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:screenOrientation="portrait">
But it doesn't work on my device (HTC Desire). It switches from "portrait" lo "landscape", ignoring the lines from the manifest file.
After more forums reading, I tried to add this in my manifest file:
<application
android:debuggable="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:configChanges="orientation"
android:screenOrientation="portrait">
and this function in my activity class:
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
But again, no luck.
If you are having a lot activity like mine, in your application Or if you dont want to enter the code for each activity tag in manifest you can do this .
in your Application Base class you will get a lifecycle callback
so basically what happens in for each activity when creating the on create in Application Class get triggered here is the code ..
i hope this helps.
Something to complement: I have updated an app recently, the previous was working in both landscape and portrait mode, and I want the updated version should work in portrait mode, so I added
to the corresponding activity, and it just crashed when I tested the update. Then I added
too, and it works.
I think you want to add
android:configChanges="orientation|keyboardHidden"
to your activity? Otherwise the activity is restarted on config-change. TheonConfigurationChanged
would not be called then, only theonCreate
Set force Portrait or Landscape mode, Add lines respectively.
Import below line:
Add Below line just above
setContentView(R.layout.activity_main);
For Portrait:
For Landscap:
This will definitely work.
According to Android's documentation, you should also often include
screenSize
as a possible configuration change.Also, if you all include value
keyboardHidden
in your examples, shouldn't you then also considerlocale
,mcc
,fontScale
,keyboard
and others?..