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
Related
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.
I am currently trying to "hook into" the orientation change event of Android so that I can perform some actions during this event, im currently using the onConfigurationChanged and android:configChanges="orientation|screenSize" combo as per the docs but the problem with this is that it seems to require you to handle all orientation actions manually or they wont happen at all once your "hooked into" it.
All i want to do is have the normal orientation change events occur but with my additional ones tacked on, is there anyway to do this?
Most of the stuff handles automatically, like resizing layout, repositioning ActionBar etc, so you don't need to "rotate" anything manually.
What you need to handle manually is:
having different images / texts for portrait and landscape mode
having different layouts for portrait and landscape mode
having different configurations (like number of GridView columns)
etc
If you don't have the stuff above specified for different orientations, there is nothing for you to handle.
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
//do your stuff here
}
However if there are different resources you have to apply them manually. Try it out as it is and then you will see what handled automatically, and what is needed to be re-assigned by you.
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 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.
I use data of the accelerometer in my app. If the device´s angle is changed the screen orientation should change.
In order to change the orientation I have 2 layouts, one for portrait and one for landscape.
When the orientation changes a function is called that changes the layout with:
setContentView(R.layout.landscape); or setContentView(R.layout.portrait);
This works fine but I have a problem with several UI elements like buttons or ToggleButtons.
I initialize a onClickListener in the onCreate Method for them and each time the orienation gets changed I initialize a new onClickListener.
Unfortunately I can´t change the state of ToggleButtons anymore.
How can I solve this problem?
you can use /layout-land and /layout-port and the phone will automatically switch them for you on orientation change. see
http://developer.android.com/guide/practices/screens_support.html#DesigningResources
as for the toggle buttons, see onRetainNonConfigurationInstance()
http://developer.android.com/reference/android/app/Activity.html#onRetainNonConfigurationInstance%28%29
As described by Bill Gary above, why not just let the system take care of orientation changes for you? Also the standard system widgets should automatically preserve their state when you do this. See here for why and how to manage custom state, if you need to.