78

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?

10 Answers 10

133

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"
1
  • 10
    or if you already have tools:ignore in application tag then set as tools:ignore="GoogleAppIndexingWarning,LockedOrientationActivity"
    – Mystery
    Commented May 4, 2020 at 9:31
25

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

1
  • 12
    - Hey! Engineers want to restrict app using in landscape mode. Lets give them screenOrientation tag. In a while: - Hey! Users want to control orientation with phone rotation. Lets restrict using of screenOrientation tag. Commented May 13, 2020 at 17:56
11

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.

7

Add following line after android:screenOrientation="portrait"

tools:ignore="LockedOrientationActivity"

Then click Alt+Enter

enter image description here

2
  • Can you explain why? Commented Jun 13, 2021 at 9:51
  • I can't ... This error is fixed in newest version of Android Studio ... It was recommendation from Android Studio yellow dot ... Commented Jun 18, 2021 at 23:37
6

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
1
  • This seems the right answer to the problem. You should have the option of turning off this warning, and the logical place would seem to be under Preferences. But the first option did not exist on my Mac Android Studio and the second stopped the app running. The tools:ignore command does the same thing but it can sit in the code next to the screenOrientation command that triggered it. maybe this is why they are pushing us this way. Commented May 12, 2022 at 11:34
5
tools:ignore="GoogleAppIndexingWarning,LockedOrientationActivity"
1
  • 2
    Can you please explain as well what is the use of this code.
    – Rahul
    Commented Feb 26, 2020 at 7:30
4

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.

1

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.

0

Try with below code:

if(MainActivity.this.getResources().getConfiguration().orientation== Configuration.ORIENTATION_PORTRAIT){
    MainActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
}
2
  • 1
    Please add some explanation to your answer such that others can learn from it
    – Nico Haase
    Commented Apr 12, 2020 at 12:32
  • if orientation portrait locked the orientation of that activity. which means orientation never changed to landscape. Commented Apr 13, 2020 at 13:17
-2

Try putting the following in the activity tag

android:configChanges="orientation"
android:screenOrientation="portrait" 
2
  • Not help. It's portrait and landscape mode
    – Alexei
    Commented Oct 23, 2019 at 9:08
  • Updated the answer you will have to add both
    – Swayangjit
    Commented Oct 23, 2019 at 9:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.