I would like to change the layout after the screen orientation changed without reloading the app.
I added the following into AndroidManifest for not reload the app after orientation changed and it worked:
android:configChanges="orientation|screenSize"
But now, I would like to change the layout when the orientation changes WITHOUT reloading app.
For example for layout-land, I have example1.xml and for layout-port, I have example2.xml with other different code.
Related
I have one Fragment which has two layouts with the same name in different folders (layout and layout-land ) when device orientation is changed fragment display always portrait layout in manifest i have this property for activity
android:configChanges="fontScale|screenSize|orientation|locale"
When you use android:configChanges="orientation|screenSize" in your manifest the activity is not recreated when device orientation changes and the layout continues the same.
If you want that onCreate to be called again on orientation change don't use those options in manifest.
I wanted to ask about android orientation;
I made 2 different folders for layout: layout and layout-land
when the orientation changes while the application is running i don't want to restart the running activity so i fixed that problem by adding the following line of the manifest file
android:configChanges="keyboardHidden|orientation|screenSize"/>
the problem then is that the app is not switching to the appropriate layout when i rotate the phone
does someone have any idea about the best way to handle the orientation changes of an android application ( i want to keep the state of the layout when i switch )
Simple: Let Android do the hard work.
Saying configChanges="orientation" overrides the default orientation behavior especially if you're using multiple layout files.
Change this:
android:configChanges="keyboardHidden|orientation|screenSize"/>
To this
android:configChanges="keyboardHidden|screenSize"/>
To simplify, I'd like to create an activity which consists of two different views (e.g. button and image). And I want only one of them to rotate on orientation change while other to be orientation-independent.
I've found a lot about iOS case but nothing about Android.
Any help would be appreciate.
It's very simple, just add screenOrientation to the activity (the one you don't want to rotate on orientation change) in the manifest file like this:
<activity android:name=".Activity"
android:screenOrientation="portrait" />
All other activities will automatically rotate on orientation change.
You can control what happens during orientation change on each activity.
OrientationEventListener
Make two layout file and change int it as per your requirement and put in layout-land and layout folder.
layout-land for Landscape View.
layout for portrait view.
I have created two layout files (in res/layout and res/layout-land) for my application. But it is not displaying the proper layout file when i change the device orientation. It just displays the same layout file (res/layout) in both the modes. Why it is not picking up the landscpe mode layout file in landscape mode? Also i have added android:configChanges="keyboard|orientation" to my activity in manifest. What else i have to do? I'am a newbie to android app development.
If you are adding added android:configChanges="keyboard|orientation" in Manifest file it won't change the layout, because by adding this attribute your Activity is not re-created again so it displays the same layout as in portrait mode. So, remove android:configChanges="keyboard|orientation" from Manifest file to get it working.
If you want that your Activity should not be created again and also it should change the layout when your orientation changes you have to add code for changing the layout manually in onConfigurationChanged() check my answer here for getting it working.
You should not add this.
android:configChanges="keyboard|orientation"
in your manifest.. because it is not allowing the call for onConfigurationchanged method... which is responsible for handling the orientation changes... remove that line from you manifest and it should work...
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.