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

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

Related

Configure Portrait Mode at one place

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.

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);

Screen orientation of an android application

Is it possible to write code on android manifest file that will automatically change screen orientation according to the device screen size?.
May this help you:
Instead of fixing the orientation on the manifest, you could use setRequestedOrientation. On the onCreate method of each activity, check if you are on a tablet or smartphone and set the desired orientation.
Another option could be Creating Multiple APKs for Different Screen Sizes, depending on your needs.
As far as I know this cannot be done in the manifest. However, you can write some code to achieve this. Have a look at the setRequestedOrientation method of Activity. Details in the documentation.
However, this will probably not generate the best user experience.
AFAIK Tablets have default orientation set to Landscape.
So this should automatically handled
Write line given below in your defined Activity in Androidmanifest.xml,
android:screenOrientation="unspecified"

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