Configure Portrait Mode at one place - android

Is there a way to set Android Portrait mode on an application level like how we do in iOS as shown below ?
I do not want to add
android:configChanges="orientation"
android:screenOrientation="portrait"
on each Activity entry of the manifest
or add the following code
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
in each activity!
Thanks in advance..

You can't do this the same way you do it in iOS. The closest you can come to avoiding boilerplate code in each activity, is to create an Activity, let's say PortraitActivity, where you set orientation:
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
in onCreate and make all your activities extend this activity.

configChanges is unnecessary for requesting portrait orientation, but you have to add screenOrientation="portrait" at every activity on manifest or "portraitSensor" that will give both portrait and portraitInverse.

Related

How to show my application all activity layout in landscape mode using landscape flag only once

I have an application which contains multiples activity. I want to show all the activity layout in landscape mode for my applicaiton.
There is a common way to do that, which is declaring android:screenOrientation="landscape" flag for each activity in android manifest file.
But this is weird when I have lots of activity and declaring that flag for all activity tag where I want to show all activity in landscape mode by default.
Is it possible or any way to declare that flag only once either manifest or activity to show all of my application activity layout in landscape mode?
You can create BaseActivity and add layout type which you want
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
or
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
Extend BaseActivity to all activities class.
There is not any a common way to do that, which is declaring android:screenOrientation="landscape" flag for each activity in android manifest file.
You have must apply android:screenOrientation="landscape/landscape" to each an every activity in your AndroidManifest.xml file
or
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
or
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);

Android Studio - landscape only but still rotate

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>

Android , Set orientation for entire app in java (include fragments)

I'm checking the size of the device and if it's not tablet I'll set orientation to portrait so I can't set orientation at Manifest. I'm checking the size and orientation at each Activity like this:
if(Services.isMobile()){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
But it has two problems 1.I have to do like so an all activities (and there is a lot of them)
2.It doesn't work for Fragments.
You should add android:screenOrientation attribute to <activity> declaration in your manifest. For more details on what values the attribute accepts, see documentation
You must declare it in the manifest file of every activity like
android:screenOrientation="portrait"
Or you can create a base class and make it abstract
StackOverflow portrait mode only
You can perform that programmatically :
You should test in all activities, or create a class and implement it in every activities.
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Use it in FragmentActivity & it will work on fragments. Just be sure to have a correct layout

orientation in activitygroup with tabbar

orientation change is not working when I rotate a child activity to landscape. I added android:configChanges="orientation|keyboardHidden in all activity tags in my manifest.xml file.
I found that my app doesn't take layoyt file from layout_land folder. what is the solution for this problem.
By putting that in your manifest you are saying that you will manually handle all orientation changes. You'll have to setContentView again onConfigurationChanged OR remove it from your manifest and it will load the right layout.
Edit: found this answer afterwards which explains it

putting whole application in landscape mode in manifest

The line android:screenOrientation="landscape" in each activity in manifest file will put that activity in landscape.
Is there any way to put whole application in landscape in manifest? I put it in application field but didnt work.
I have an app which will only use landscape mode.
Nope. You might somehow embed it into the code before you launch your activities, but overall, the activities specification must have that setting. Look at the specification here and here.

Categories

Resources