Landscape and portrait android app - android

I have created two folders layout and layout-land with two xml files, one for portrait and the other for landscape. Both of the xmls work but here is the problem.
My first screen is a login screen and my second screen is a main screen. If I login in portrait and then turn my phone landscape at the main screen. The layout will landscape turn but it uses the portrait xml for the main screen.
The same error occurs if I start in landscape and try to move to portrait later on.
It seems like whatever layout I do for my main then that's the layout that will be used for the rest of the app. Is there anyway to go around this?
Also. I'm already using android:configChanges="orientation" in my manifest for the activities.

If you are using android:configChanges="orientation", then you can override onConfigurationChanged to inflate the new layout after a configuration change.
#Override
protected void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(...);
}
Make sure you have a good reason for preventing the Activity from being recreated on an orientation change... and most importantly don't just do it because orientation changes are crashing your app. Handling the configuration change yourself can make it much more difficult to use alternative resources, because the system does not automatically apply them for you. This technique should be considered a last resort when you must avoid restarts due to a configuration change and is not recommended for most applications.

Using android:configChanges="orientation" means you will be handling the orientation change in code. If you want it to automatically switch layouts, you shouldn't have that there.

Make sure both xml files present in two different folders have same name.

Related

How to use layout and layout(land) xml files correctly?

I have an editText and some buttons within an activity. Every time I tilted the phone to landscape mode I reset it to the standard value. To change this, I added this line to the activity manifest:
android:configChanges="orientation|keyboardHidden|screenSize"
And with this I got the same values in it changing phone orientation. But my layout was perfect in portrait mode and awful in landscape mode(buttons were off screen). So I got two xml files: layout and layout(land). In this way, if the phone is tilted when this activity is called, layout(land) is called. Otherwise, the portrait layout is called. But every time I tilt my phone after the activity was called the layout used keeps the same. I would like to know how to dynamically change layout during activity call and after it.
You are preventing the activity from reloading by specifying android:configChanges in the manifest.
Removing this will allow the activity to reload on rotation change and therefore handle the layout correctly. You will then need to handle the life cycle events of the activity to save and restore transient data during the rotation.
https://developer.android.com/training/basics/activity-lifecycle/recreating.html#SaveState
Normally the layout file in /res/layout are applied to both portrait and landscape.
For example,
/res/layout/activity_main.xml
you can add a new folder
/res/layout-land, copy activity_main.xml into this folder and do necessary adjustments
Make Sure the name of the folder should be layout-land for landscape mode.

android reload layout resource programmatically

I have an app in which I override the onConfigurationChanged because I don't want the activity to be destroyed and rebuilt. it works fine.
Now I want a certain part of the layout to change when the orientation is changed. Regularly I would do this with the layout-land folder and that would work, but now it doesn't. The portrait layout stays even when I change to landscape, though it is obviously redrawn, because the items in it change their locations accourding to the new orientation limits.
I think that's because the layout is only redrawn but the resource isn't re-selected from the res folders.
Any idea? (I wouldn't want to use setLayoutResource or something alike, I would rather to use the more general solution).
thanks.
Without onConfigurationChanged android redraw the scene by it's own (it setLayouResource according to the orientation).
When you write onConfigurationChanged you say to OS that you will handle all thing by hands and with onConfigurationChanged android only rotate the corresponding view. If you want to use layout from layout-land the only way is to call setLayouResource (or move views (change it's layout parameters) dynamically - but this way is not very good) and restore all of your data.
In AndroidManifest insert the parameter: android:configChanges="orientation" on the activity.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.yourlayout);
}
and provide two versions of yourlayout.xml: layout/yourlayout.xml and layout-land/yourlayout.xml. You can add more - for different sizes, etc.
Don't forget to set android:configChanges="orientation|screenSize", otherwise your activity may be reloaded.

Different activities for landscape and portrait

I have no idea of this is actually possible, but i want the device to have different activities for landscape and portrait.
I have a listview, and of all the items in my listview i have the coordinates. So i thought, it would be nice if you put your device in landscape, and show a mapview with icons placed on the locations of the items.
I know you can create different layouts for different orientations, but only creating an mapview and using that as landscape layout wont do the trick i guess. Is there something possible in an equivalent way for activities? I couldn't find it, so probably not.
Else i think this might work:
I thought myself of a switch in the activity: on portrait --> do this and load this layout, on landscape --> do that and use the other layout. But this would only work once if put in the oncreate. But than the orientationlistener would do the trick. Anyone knows of this is possible?
There is a method in activity lifecycle
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Check the Orientation of Device
// Start Other Activity
...
...
...
}
you can use this method to change the activity if the device orientation changes.
IMHO we can't use 2 different activities here, but there is a way,
please check out how to detect orientation dynamically here,
check-orientation-on-android-phone
once you are through this, you can include code for landscape mode here in the same activity.

How to start a new activity when screen orientation changes? Android

I am writing an application which in portrait view has a gallery at the top and when you click a picture it will inflate and fill the entire screen.
This works however in landscape mode the gallery covers up most of the picture and it in general looks crappy.
I made a GridView for landscape mode. The problem I am having now is to change it from the gallery activity to the Gridview activity when the orientation changes. Any ideas?
The right way to do it is to define two layouts, one in layout-port and one in layou-land directories. That is, you will have the following two layouts:
res/layout-port/main.xml and res/layout-land/main.xml.
In your software you simply write secContentView(R.layout.main); and android will take care of applying the right layout upon device rotation.
Make an extra layout xml file for landscape mode and put it into the folder "layout-land"
Add this to your Activity:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
startActivity(newActivity)
}
Note that this is a bad idea. Actually, it's a really bad idea. A much better alternative would be to either force one orientation or to create a layout that looks great on both orientations.

Recreating WebViews while changing the orientation in android

I have built an application and the best part is, it is running fine and the worst part is,whenever I hold the device and turn it from portrait to landscape or vice versa,the views are regenerating each and every time when the orientation changes.I have done all the possible things to the best of my knowledge i.e.
1.created layout-land folder and placed the xml file for the landscape mode.
2.Have given the following permission in the manifest:
android:configChanges="orientation|keyboardHidden"
Is there anything else I have missed out?
I do not want the changes or recreation to occur whenever I change the orientation.
By default,Android activity is recreated when orientation is changed, and xml layout used by default is xml that you have in "layout" folder unless you have a separate xml in "layout-land" for landscape mode.
On the other hand if you want to handle Orientation or any other config changes by you self and to avoid calling OnCreate() then use "android:configChanges" in manifest file and Overide OnConfigChanges in your activity.
The reason that it is recreating your views is because the oncreate is being called. This will ideally reset all your views. If you override the method that handles the orientation changes, you will catch it a lot easier and preserve your views.
Ex:
http://jnastase.alner.net/archive/2010/10/27/handling-orientation-change-in-android.aspx

Categories

Resources