Different layout on different screen orientation without onCreate - android

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.

Related

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

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.

Orientation Change - Update UI

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.

Android - Handle Screen Orientation

I'm currently working on an Android application that have different behaviors according to the screen orientation.
I'm using getWindowManager().getDefaultDisplay().getRotation() to get the screen orientation in the onCreate() method and manage from there.
Basically, my problem is that I need a control over the orientation. In landscape mode, when the user decides it, the application must switch to portrait mode, and stays like that, but only until the use goes to portrait mode, and then again to landscape.
I'm using setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) to change the orientation when in Landscape. The problem is, using this, I can not go back to landscape.
If I use setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) in onCreate(), the problem is that is switches back to landscape mode after a few seconds (since the screen is still in landscape).
Is there any other way to manage this? Basically, is there a way to catch the screen orientation change with a listner (or anywhere else than in onCreate, since it's not called when the views is locked in portrait mode)? I can't find anything about that...
Any idea is welcome!
Thanks!
Why not try creating two layouts, one in layout-land and one in layout-port. These will automatically be applied when the device orientation changes.
Inside each of these, create two distinct child layouts one for a portrait view, and one for a landscape view and change the visibility of these to reflect the user's preference.
So, you don't need to handle the rotation change at all, let Android manage that for you, and you just manage the user override.

Categories

Resources