Android Studio - landscape only but still rotate - android

I want my android studio app to be landscape only, but to switch orientation when the phone is rotated.
This means that when u flip the phone 180°, it will still be landscape but will flip sides. Like the clash of clans game.

You should add the sensorLandscape screenOrientation tag to your activity in the android manifest file and you should be good to go.
android:screenOrientation="sensorLandscape"
Make sure to add this to all the activities that you want to have in landscape mode.

All you need is to go to your AndroidManifest.xml file and under each Activity add the screen orientation tag and set it to sensorLandscape. Make sure you do it for all the other activities if you want them to be in landscape too.
For Example :
<activity
android:name=".MainActivity"
android:label="#string/title_MainActivity"
android:screenOrientation="sensorLandscape">
</activity>

Related

In AndroidManifest: Expecting android:screenOrientation="unspecified"

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"

Landscape layout file is loaded, while portrait mode is forced. (Android)

I'm currently developing an app, in which phones have forced portrait mode, while tablets support portrait mode as well as landscape mode. Some pages the layouts have respective landscape versions in the layout-land folder.
The problem
While using the app on a phone (so portrait only) and when autorotate on the phone is enabled, the landscape layout file gets loaded while the app is displayed as portrait mode.
When does this occur
To recreate this behaviour I open an activity and rotate the device. Then when I navigate to another activity, or close and open the application, the landscape layout gets loaded.
Method of forcing portrait mode
To force portrait mode on phones, I call
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
in my BaseActivity onCreate method.
Other details
targetSdkVersion: 26
using support library 26.0.0
Thank you for helping!
Edit 1:
Unfortunately using (a combination of) the following settings in the manifest file gives the same behaviour.
android:screenOrientation="sensorPortrait"
android:screenOrientation="portrait"
android:configChanges="orientation"
Edit 2:
I've now tested on multiple devices (emulator, One plus, Motorola, Samsung and some more) and this oddity only happens on a Samsung A5 (api 23) for me.
Is other Activities are also have that code?
I meant, other Activity that doesn't extends BaseActivity?
I would prefer to set in AndroidManifest.xml file in each activity tag like >> android:screenOrientation="portrait"
add this line android:screenOrientation="portrait" in your project manifest file inside activity. Hope it work well.
Try this
<activity
android:name=".Activity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/title_activity_test_edit"
android:screenOrientation="sensorPortrait"
android:theme="#style/Theme.DesignDemo"
android:windowSoftInputMode="stateAlwaysHidden" />
Portrait orientation, but can be either normal or reverse portrait based on the device sensor.
Refer this link for more details :
https://developer.android.com/guide/topics/manifest/activity-element.html
As you mentioned,
phones have forced portrait mode, while tablets support portrait mode
as well as landscape mode
Set screen orientation in manifest will not work.
Use layout-land-sw600dp, layout-land-sw720dp folders for landscape view for tablets. Also please take a look at this SO thread

Definition of android device natural orientation

I want to change the basic orientation of an android device. Curently its startup orientation is landscape, I need portrait.
I managed to modify the acceleration values so that the axis now point to where I want them to point to.
But the device seems to have its own definition of up and right. Now I have to change this.
Can someone tell me where this can be done? I guess there is a config file out there that I can't identify.
There are millions of hits searching for this problems. Unfortunately they all deal with java programming, not with OS code.
In your manifest file under activity tag that is on which screen you want to apply that orientation.
For portrait
android:screenOrientation="portrait"
For landscape
android:screenOrientation="landscape"
Add this lines of code in Androidmanifest.xml for the orientation
<activity android:name=".SomeActivity"
android:label="#string/app_name"
android:screenOrientation="landscape">

Layout doesn't rotate automatically

Hi I already made two XML layout file with same name, one in layout folder and the other in layout-land.
I already add the configChanges tag in Manifest, like this:
<activity
android:name=".Main"
android:label="#string/title_activity_main"
android:configChanges="orientation|screenSize" >
...
</activity>
The layout won't change automatically after rotating my phone.
But when I started the app in Landscape orientation, it will use the layout-land xml and keep using it even after I rotated my phone into portrait. So, there is no error in the landscape code.
I'm using Android 4.1.2 for testing. Is this the problem?
Or do I need to add some code in the Activity class?
Thanks
[Update]
I tried this in freshly-made project. But the problem still the same
Remove android:configChanges="orientation".
Using this attribute means that you will handle the rotation yourself, but you want the OS to handle it for you.

How to avoid changing of image and layout position when changing from portrait to landscape mode in android

I have problem while using lanscape mode in android.How to avoid changing of image position changing from portrait to landscape mode in android.Normally landscape4 and portrait mode is good.If we goes from one mode to another then position of images are changed. Please help me
The activity in which you are showing image, register that Activity in Android Manifest file as:
<activity
android:name="com.android.YourActivity"
android:screenOrientation="portrait">
</activity>
In this way, it will force the activity to stay in Portrait mode only.
The orientation changes causes the running activity to restart.The restart behavior is designed to help your application adapt to new configurations by automatically reloading your application with alternative resources that match the new device configuration. Hence resource intensive.
Depending on how you want you app to behave and the device performance you can handle the orientation change(config changes as a whole) in following ways:
Fixed orientation: If you want your app to have a fixed orientation in landscape or portrait mode. For this you need to set this attribute in the manifest file under the activity tag.
android:screenOrientation="portrait"
or
android:screenOrientation="landscape"
Handling the Configuration Change Yourself: You can handle the orientation change yourself if your application doesn't need to update resources during a specific configuration change and you have a performance limitation that requires you to avoid the activity restart, then you can declare that your activity handles the configuration change itself, which prevents the system from restarting your activity.
For this you need to declare this in the manifest file:
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name"/>
and you need to override the onConfigurationChanged() method of the activity.
** use this as the last resort as every thing needs to be handled by you.
Default behavior: You can let android handle the config changes choosing the alternative resources from the resource folder. You just need to provide these alternate resources for landscape and portrait mode.

Categories

Resources