I created layout and layout-land folders, in Manifest i have
android:configChanges="keyboardHidden|orientation"
If i remove the android:configChanges from the Manifest I can see the layout change from portrait to landscape...the problem is that the previous screens are destroyed when trying to go back.
I am assuming i could add some code in my Java to tell it when to change layouts. I have read a bunch of posts already but am confused where in the code it would happen.
I am trying to use some prepackaged code because I am new to Android. The link below is the Java code for the menu screen in particular i need to change layouts for. thanks so much!!
menu Java
Adding android:configChanges="keyboardHidden|orientation" will stop re-creation of your Activity and also prevent from changing the layout to layout-land/portrait layout. In that case you have to manage it manually from the java code. If you don't want your Activity to get re-created when you rotate your device/ change orientation. What you can do is just change the layout when your screen orientation changes inside onConfigurationChanged() method,
Check my answer here, how can that be done.
My work around for these problem was
in manifeast file you can use android:configChanges="keyboardHidden|orientation" if you wont these line then activity will be destroyed and created again.
and we have a below method
#Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
setContentView(R.layout.yourlayoutname);
}
here you can handle the orientation change...
Related
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.
I am new to Orientation in Android. I have worked only for portrait mode so far, now I am working for both orientations.
I am using this method in manifest file for orientation :---
<activity android:name=".Activity_name"
android:configChanges="orientation|keyboardHidden">
The Problem is :-
I have two layout Folders for this - one is layout & other is layout-land for both orientations.
Now I run my app sometimes it takes layout xml file on both orientation & some times layout xml file & layout-land xml file both works .
How to resolve this problem?
& If i use this code :--
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
setContentView(R.layout.login_landscape);
}
else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
setContentView(R.layout.login);
}
}
Then orientation problem gets resolved but Activity is not created again, so my code works only one time . My Button is not working now.
2. My second question is that For Both XML File How to manage Code in Config Method
I want to know the complete concept of Orientation .
Answer will be highly Appreciated.
Thanks & Warm Regards,
Deepanker Chaudhary
layout is same as layout-land for portrait orientation it is layout-port folder. There is no need for you handle the layout file for orientation changes. All you need is to save the data on orientation change and restore it when displaying it. You can use the same file name for both the orientations. You can get some help from here for orientation handling. check, also this.
Please make sure below point :
android:configChanges
Lists configuration changes that the activity will handle itself. When a configuration change occurs at runtime, the activity is shut down and restarted by default, but declaring a configuration with this attribute will prevent the activity from being restarted. Instead, the activity remains running and its onConfigurationChanged() method is called.
I have an activity with two layout files, one each for portrait and landscape modes. I see that the onCreate method is called each time I change orientation. I tried using
android:configChanges = "orientation"
in the manifest file, but it only reads the portrait layout. Is there a way I can set the layout to the landscape mode without calling onCreate?
If you use android:configChanges = "orientation", you must override the onConfigurationChanged(...) method of your Activity. Check the newConfig parameter for the new orientation then manually set the layout you want.
See the following links:
onConfigurationChanged(Configuration newConfig)
Handling the Configuration Change Yourself
You could create two layout folders. One for portrait mode and one for landscape.
layout and layout-land.
This is a fact of life with android. You need to develop your activity around the fact it can be destroyed at any minute due to a configuration change like the screen's orientation changing.
Save all the the information used to build the screen in on pause and recreate it in onCreate.
If you want your activity be only in landscape mode, you set this attribute for the activity in the AndroidManifest.xml:
android:screenOrientation="landscape"
You can put your layout.xml files in such folders for different screen orientations:
res\layout-land
res\layout-port
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.
i've developed an android app that's fetches an xml file and displays this data via several markers on the map. This works fine so far.
The problem right now is that when i switch the orientation of the phone (portrait->landscape or vice versa) the markers disappear for a small moment, the xml processing is started again and then they reappear.
Is there a way to prevent this re-loading of the file? It only takes about 2-3 seconds..so no big deal, but still disturbing
Have you tried to override OnConfigurationChanged() method in your activity? if not then try below snippet
#Override
public abstract void onConfigurationChanged (Configuration newConfig) {
super.onConfigurationChanged(newConfig);
//Do nothing here
}
also in your manifest file, add following attribute to your activity
android:configChanges="orientation"
You can force the layout to be only Horizontal or Vertical and avoid the reload on layout change with this:
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
this refers to the Activity.