i have developed an application that works only with a portrait layout.
Actually it has only one layout resource used for all screens.
Now i want that with some screens (especially tablet screens) the layout is different from other devices, and especially i want to use a fixed landscape orientation.
Actually in the manifest i forced the main activity to have portrait orientation:
<activity android:name=".launchActivity"
android:label="#string/app_name" android:screenOrientation="portrait" android:clearTaskOnLaunch="true" android:finishOnTaskLaunch="true">
Now how i can manage to have 2 different "fixed" orientation?
It can be done via xml or i need to handle them programmatically?
Handle it programmatically. use setOrientation method.
Related
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
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>
I got an activity which I want to always remain with portrait layout. I got these settings in the manifest file:
<activity android:name="Main"
android:launchMode="singleInstance"
android:label="#string/app_name" android:screenOrientation="portrait" android:configChanges="keyboardHidden|orientation" android:windowSoftInputMode="stateAlwaysHidden|adjustNothing">
This settings makes the activity just load the portrait layout to the side if I turn the device on the side. I want the layout to just remain in the portrait position and that's it even though it means viewing the layout on the side when switching the device 90 degrees.
From Android 3.2 onwards, you also need to add screenSize to configChanges
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation|screenSize"
From the developer docs at handling runtime changes
Caution: Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation.
Force it dynamically:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
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.
I discovered by accident that if you have an Activity that is set to one orientation, but contains a separate layout file for a different orientation, when the Activity is first started it will be inflated with the layout file that matches the device's physical orientation and not the layout file that matches the Activity's orientation. Thus if AndroidManifest.xml specifies
<activity
android:name=".activities.LandscapeGardener"
android:screenOrientation="sensorLandscape">
<!-- note for new readers: sensorLandscape is like landscape, but also
works upside-down.
Other activity stuff would go here. -->
</activity>
and there is a layout for LandscapeGardener in the layout-port folder, if the Activity is loaded in portrait, the portrait-specific layout will be inflated.
The reason we have this situation, by the way, is that an Activity we're developing has some portrait-specific bugs, so we decided to suppress portrait mode for users while we fix them—but of course this problem makes the suppression somewhat partial!
Note that if the device is correctly oriented at startup, it will not change to the incorrect layout.
instead of sensorLandscape, try just landscape. i'm pretty sure it works.