Android can use wrong layout file for single-orientation activities - android

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.

Related

how to fix orientation issues on android

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"/>

How to avoid changing of image and layout position when changing from portrait to landscape mode in android

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.

Screen orientation in android

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...

how to adjust the proper interface for both portrait and landscape mode for xml files in android

My problem is that I prepare my xml in portrait mode in Android but when I try to run the application with phone and get the landscape mode, my interface crash. Picture gets smaller and buttons don't appear on the screen. I want my interface to be the same whether the phone is in landscape mode or portrait mode.
How can I solve this problem? Can I prepare two different xml for one class, one for landscape and the other for portrait mode or is there any other solution?
Thanks
You have two options here. The first is to add the android:configChanges attribute to that activity in the manifest:
<activity android:name="the activity's class name" android:configChanges="orientation" >
This will stop android from recreating the activity when the orientation changes. Instead the activity's onConfigurationChanged method will be called.
The other is to make another layout that will be used when the device is in landscape. It would need to be named the same as the portrait layout and put under layout-land.
There's probably other options as well, these are just the ones that I know of.
You can easily have different layouts in different orientations, see "Screen Orientation" in Providing Resources
All you need to do is create a layout-port or layout-land directory under res, and put the alternate XML in there.

screen orientation

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.

Categories

Resources