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.
Related
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.
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
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"
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.
Hi
M new to android.I faced a problem i.e in landscape mode i need a layout view which doesnot have some feilds which are present in the portrait mode.I have created a layout for landscape view in such a way.In the activity i have given the conditions like if portrait display some feilds some hide.its working properly actually...first when i go from portrait to landscape it is giving what i want...but when i go to portrait mode its stopping the application.please help me.Thanks in advance
You can make two different layouts and put them in layout-port and layout-land respectively.
And make sure that you have mentioned android:configuration = "orientation" in your manifest for that activity. Try it if i have understood you correctly.
I think that the advice by Kantesh may be backwards. As explained in the docs, if you include android:configChanges="orientation" in the manifest, then the correct resource (from layout-port or layout-land) won't be automatically loaded. Instead, leave out mention of orientation from the manifest. Then, you do not need to worry about onConfigurationChanged() (don't override it). The system will automatically shut down the activity and then restart it, binding the appropriate version of configuration-dependent resources. Handling configuration changes yourself is (again, according to the docs) only a last resort to deal with performance issues that cannot be handled in other ways.