android reload layout resource programmatically - android

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.

Related

Different layout on different screen orientation without onCreate

I want two different layouts (one for portrait and one for landscape), but when I change the orientation, I don't want to repeat onCreate.
Is it possible to do so?
Normally when changing the orientation onDestroy() and right after that onCreate() of the Activity is called.
However you can avoid this: Handling Configuration Changes Yourself
EDIT: For a different layout for portrait or landscape mode you can use two layouts in onCreate() and pick one based on the orientation (I know you wanted to avoid the onCreate method, but I don't think it is possible otherwise). Just check the current orientation and with this.getResources().getConfiguration().orientation act accordingly.
Maybe you can avoid using onCreate() and do the same thing in onConfig but I am not sure about that.

Config changes ScreenSize and Orientation stop layout changing

I have an android layout - main.axml, which changes based on orientation. I have overriden the OnConfigurationChanged so that I can adjust the way some icons (which sit outside of this layout) look based on other criteria. I have configuration changes set to Orientation and ScreenSize, and here's my method:
public override void OnConfigurationChanged(Configuration newConfig)
{
base.OnConfigurationChanged(newConfig);
toggle.OnConfigurationChanged(newConfig);
animateDrawerIcon();
FabController();
}
The problem is, the layout no longer changes, it stays the same regardless of orientation, if I put SetContentView(ResourceLayout) in the method above, the layout changes but is completely empty.
I really want Android to handle the rotation/screensize change, I just want to call two functions when that happens - can anyone advise how best to do that?
If you want to Android to do it, then the easiest way is to define two layouts for two orientation. This is ideal if the layout in landscape is quite different from what it looks in portrait mode.
But if it is a few bits here and there, then you should detect the current orientation of the device and toggle the buttons and switches (or whatever you want to do !!!) in your onCreate method. In this case, you need not define multiple layouts.
For.e.g if you have a button which you want to be only in portrat mode, then let the button be visible by default and in onCreate of your Activity, detect the orientation by - getResources().getConfiguration().orientation and if the orientation is landscape then just toggle it.
Although OnConfigurationChanged method is really convinient, Avoid putting a lot of logic in it. Try either of the approaches mentioned above

Avoid reload view after orientation change

I am new to android development . I have been puzzled by this problem in recent times .
Every time orientation changes my view gets reloaded. I want to avoid this. This is happening on change from landscape to portrait and vice versa.
Put below line in your manifest in activity:
android:configChanges="orientation|screenSize"
So, your activity should look like:
<activity
android:configChanges="orientation|screenSize"
android:name=".YourActivity"/>
This way prevent destroying activity on orientation changes.
The reason why Android is reloading the view after the orientation has changed is that this view is most probably another view than the one that was displayed before the orientation change happened, e.g. with new layout, font sizes, imageviews or other resources. The Android system is able to automatically load suitable resources for different screen dimensions (e.g. height, width, density ... ) in case of an orientation change.
For this to happen, Android first calls the activity's onDestroy() method, followed by a call to its onCreate() method to enable the activity to reload resources and activity state.
If for some reason you do not want this behaviour, you can follow the above advice and add an android:configChanges attribute to your activity element. In this case Android calls the activity's onConfigurationChanged() method instead of onDestroy() and onCreate(). onConfigurationChanged() is the method to place your code to adapt to the new orientation, if necessary. If not, it can also be left empty.
For further reading, see http://developer.android.com/guide/topics/resources/runtime-changes.html

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.

Landscape and portrait android app

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.

Categories

Resources