how to fix orientation issues on android - 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"/>

Related

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

Android can use wrong layout file for single-orientation activities

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.

android layout-land and onConfigurationChanged

I am a starter on Android and trying to figure out how do use different layout files (main.xml) for portrait and landscape screens.
I tried to put main.xml in both res/layout and res/layout-land, then rebuild the project. However, this doesn't work for me.
I then added onConfigurationChanged method in the code and setContextView to R.layout.main. This time I got different screen layout in different orientation, but all functionality were lost. Some posting also said using onConfigurationChanged method is not really the best idea.
So can anyone help me with this issue?
Thanks,
Go to your AndroidManifest.xml and add this line to your activity tag:
android:configChanges="orientation"
This tells Android that you wish to control the state of your activity when the orientation changes. Normally, when there's an orientation change Android will actually destroy your activity and recreate it by mean of onCreate() again, so any data that was obtained will be lost unless the state is saved in the onConfigurationChanged() method.
Android will automatically use the layout that represents your devices current state. For your specific project, the layout-land xml file should be used when the device rotates. When you override the onConfigurationChanged method and modify your manifest, it tells Android you will handle the configuration change yourself which I do not recommend, especially if you want a new layout to be loaded.
Here's what you need to do (to test for the different layouts). Put an xml file called main in your layouts folder. For the layout, just use a LinearLayout that fills the parent with a blue background. Now create an xml layout file called main but put it under the layout-land folder. In this layout, use a red background.
Start the app, rotate the device, and you should see the background change colors. This is because the activity is being destroyed and restarted with the new layout.
Try to use onRetainNonConfigurationInstance() and getLastNonConfigurationInstance() methods to solve your problem.
Also you can use above sloution given by jrobinson3k1.
Thanx.

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