I want to run my application always in portrait mode. I searched and found the code below, then I added to manifest file.
android:configChanges="orientation"
android:screenOrientation="portrait"
but my application still chancing orientation when I press CRTL+F12 on AVD.
How can I force my application always run in portrait mode?
Ps: I tried the code reverse. (Second line first then first line)
Did you add that in the activity element, not the application element?
<activity
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden" >
</activity>
Related
Android Studio 3.6.
I want my app to be always in portrait mode. So in my AndroidMainfest.xml:
<activity
android:name=".activity.SplashActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I run the app and SplashActivity shows in portrait mode. Nice.
But the editor shows the following error:
Expecting android:screenOrientation="unspecified"
Why?
In your manifest tag (just under xmlns:android="http://schemas.android.com/apk/res/android"), put
xmlns:tools="http://schemas.android.com/tools"
Then inside the application tag, put
tools:ignore="LockedOrientationActivity"
it only affects Android Studio 3.6+
What is the Issue here?
This issue occurs because android framework wants user to control the app orientation himself it is not advised to restrict your app orientation for example if a user wants to use the app in landscape orientation he just flips the device and sensors will do the work but when a developer restrict screen orientation, even when rotation sensor works app will stay in pre-defined state, in a way you are restricting user's device capabilities.
What to do now?
You have two options.,
First is to ignore the error as it won't cause any build failure even I am doing the same and apk/aab generation is as usual
Another Option is to provide Landscape Layouts or to handle the rotation like in some apps which recognize if orientation gets changed they will prompt the user to change the orientation as the app is not supported in such orientation
It may change in future => at present it is not effecting our build process but it might change in future
In Android studio 3.6.0 i guess they want the user to handle the orientation and encourage developer to use ViewModel stuff.
Let me explain screenOrientation in detail
android:screenOrientation="portrait"
will give you error you have to either specify
android:screenOrientation="fullSensor" or android:screenOrientation="unspecified"
fullSensor Means either you have ON the "Rotate off" or not it will change the orientation based on you move the phone
unspecified Means if you have ON the Rotate off then it will stay only in that orientation and if not then it will change the orientation based on you move the phone.
Add following line after android:screenOrientation="portrait"
tools:ignore="LockedOrientationActivity"
Then click Alt+Enter
I found 2 way of solving this problem
First,
Android Studio -> Preferences (for Mac) or Settings (for Windows)
-> Search "chrome"
-> Uncheck "Activity is locked to an orientation"
-> Apply and Ok
-> Sync Project with Gradle file
Second,
Select "Run" from the main menu
-> Edit Configurations.
-> Launch options - Launch
-> Select Nothing or Specified Activity
-> Sync Project with Gradle file
tools:ignore="GoogleAppIndexingWarning,LockedOrientationActivity"
I have used the below Procedure. It works perfectly for me. In Android studio 3.6.0 I think they want the user to handle the orientation and encourage the developer to use ViewModel stuff. Use the below Procedure to ignore that.
Firstly Add :
xmlns:android="http://schemas.android.com/apk/res/android"
in the manifest tag.
Secondly, Add
tools:ignore="LockedOrientationActivity"
in application tag. Happy Coding.
I have faced this issue, In my requirement, some of the activity will support both orientations and remains will keep in portrait, In this case, I resolved by the following steps:
Case-1:- To lock Orientation
Step-1:
Please Add following line Application tag in AndroidManifest.xml
tools:ignore="LockedOrientationActivity"
<application
android:name=".activity.MyApplication"
tools:ignore="LockedOrientationActivity">
Step-2: If you want to lock screen orientation either in portrait or landscape
add the following line in the activity tag
android:screenOrientation="portrait" or "landscape"
Case-2:- Suppose if u want to allow orientation in particular activity and have a specific design for landscape
Step-1:
Remove Orientation value in configchanges and remove screen orientation if it exists from activity tag, and my personal suggestion use (LifecycleObserver) in Activity for without losing values while changing orientation.
android:configChanges="orientation"
android:screenOrientation="portrait" or "landscape"
This is the way I resolve this issue, hope it helps, Thanks & Happy Coding.
Try with below code:
if(MainActivity.this.getResources().getConfiguration().orientation== Configuration.ORIENTATION_PORTRAIT){
MainActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
}
Try putting the following in the activity tag
android:configChanges="orientation"
android:screenOrientation="portrait"
I have a problem to lock the orientation of my app in portrait mode. I added android:screenOrientation="portrait" and android:configChanges="orientation" to my Manifest. Moreover, I added setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) in the onCreate(...) of my MainActivity. The MainActivity is extended by AppCompatActivity. At any time, it works, so you never see your app in the landscape orientation, except when you install it for the first time on your device. Then it is possible to change between landscape and portrait orientation. Does anybody know why this happens? Thank you in advance.
Don't apply the orientation to the application element, instead you should apply the attribute to the activity element, and you must also set configChanges as noted below.
Example:
<activity
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden">
</activity>
This is applied in the manifest file AndroidManifest.xml.
In one of my application, there are multiple Activity(s),
Mostly all activities are configured to be in Landscape orientation by providing android:screenOrientation="sensorLandscape" for each activity
in android manifest file. But one of the activity called 'MyDashBoardActivity' changes orientation to Portrait automatically, when that Activity starts in one of my Android device, but it works fine (Loads in Landscape orientation as expected) in other devices.
How can troubleshoot this issue.
remove this android:screenOrientation="sensorLandscape"
And write In your onCreate :
after setContentView();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Try myActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
or
<activity android:screenOrientation="landscape" android:configChanges="orientation|keyboardHidden" android:name="MyDashBoardActivity"/>
I have looked around SO and found how to lock my entire application to Landscape by editing the Manifest file. However, this is not what I want.
I want every activity to be locked portrait, except for one, which will be locked to landscape.
In your manifest file, find your Activity declaration and use the following:
<activity
android:name=".ActivityName"
android:label="#string/app_name"
android:screenOrientation="landscape" >
</activity>
My app has a handful of windows. After each of those windows is created, I have code whose intent is to lock those windows in portrait mode:
var win = Ti.UI.createWindow({ ... });
win.orientationModes = [ Ti.UI.PORTRAIT ];
I still can't believe that there isn't a more universal way of doing this for Android, but that's a gripe for another day. :-) For now, this is mostly working. I'm hoping someone here can help me remove the mostly qualifier.
The app opens with a tab group containing 4 tabs. If I rotate my device as soon as the default active tab is loaded, the layout rotates as well. Oops. In fact, if I go immediately to any of those tabs and rotate my device, the layout rotates. More oops.
However -- and this is the fun part -- if I relaunch the app and jump first to a secondary window, one loaded from one of the tab windows, and rotate my phone, no display rotation. Yay! Now I jump back to any of the tab windows, the ones that were rotating before, they no longer rotate.
Wat?!
That's the pattern I think I'm seeing right now. Anyone seen this before? I'm guessing that it's a tab group thing, but I don't see any orientation accessors in the API. Any guidance would be much appreciated.
Thanks.
You'll want to add this to your tiapp.xml
<android >
<manifest>
<application>
<activity android:name="org.appcelerator.titanium.TiActivity" android:screenOrientation="portrait" android:configChanges="keyboardHidden|orientation">
</activity>
</application>
</manifest>
</android>
That will lock your entire app into portrait mode
Create a custom AndroidManifest.xml through the help of this
and add this line in Application Blog
<application
android:debuggable="false"
android:icon="#drawable/appicon"
android:label="iVite"
android:name="IviteApplication" >
<activity
android:configChanges="keyboardHidden"
android:name="org.appcelerator.titanium.TiActivity"
android:screenOrientation="portrait"/>
<activity
android:configChanges="keyboardHidden"
android:name="ti.modules.titanium.ui.TiTabActivity"
android:screenOrientation="portrait"/>
<activity
android:name="ti.modules.titanium.ui.android.TiPreferencesActivity"/>
</application>
This is right way to use. Because, In Tabbar Application. We have also need to set Tabbar Activity and its orientation Setting. Like in above code In above Code.
Here, both Activity Orientation is set through AndroidManifest.xml.
TabbarActivity and Window Activity.
Cheers....