I build my Android Application and want do add layouts for different orientations now. I created a layout-land folder and put a different layout for my first Starter Activity "myStartActivity" (with the same name as the layout i used before for both orientations) in there.
Depending on my Screen Orientation BEFORE i start the app the right layout is chosen: "myLayout.xml" from within the "layout"-folder when i start in portrait and the "myLayout.xml" from within the "layout-land"-folder when i start in landscape.
The Problem is, that when i rotate the device when I'm already in the Activity, after rotation I dont get the new layout. For example: rotating from portrait to landscape it stills shows "myLayout.xml" from within the "layout"-folder and not the "layout-land"-folder as it should.
I didnt overwrite any OnConfigurationChange Methods or anything. All I do in "myStartActivity" is instantiate some buttons and give them some listeners. I wanna use a different layout in landscape to change the ordering of the buttons.
In my case the described problem happens only when I have android:configChanges="orientation"
in the activity in the manifest.
Otherwise the correct layout is automatically used when rotating.
What you could do is use Activity.getResources().getConfiguration().orientation
Then depending on the orientation result, set the content view in your oncreate() method which is called on rotation.
protected void onCreate(Bundle savedInstanceState) {
int result = this.getResources().getConfiguration().orientation;
if (result == 1){
//set content view to portrait
setContentView(R.layout.portrait);
}
else{
//set content view to landscape}
setContentView(R.layout.landscape);
}
Or stick in a case statement :)
If you're testing on the emulator only, there may be problems with detecting orientation change. I have experienced that at least.
Related
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
I want to use a separate layout for my fragment on landscape view , hence I have created a separate layout for landscape view . But the problem is when I rotate the screen onConfigurationChanged , onDestroyView onDestroy called sequentially . And it backs to previous fragment . I can prevent it by using android:configChanges="orientation|screenSize" on my activity . But in this case , the view remains same it doesn't use my separate layout , it is just stretched to fill the screen . Is there any way so that I can use separate layout while using android:configChanges="orientation|screenSize" ?
To use different layouts use different resourse folders:
That is, use layout folder for portrait and layout-lang for landscape mode.
you can use getResources().getConfiguration().orientation to detect orientation, then load XML you like something like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
switch (getResources().getConfiguration().orientation) {
case Configuration.ORIENTATION_PORTRAIT:
setContentView(R.layout.aportrait);
break;
case Configuration.ORIENTATION_LANDSCAPE:
setContentView(R.layout.alandscape);
break;
}
/////..............
}
your issue is quite generic,
If you disable the orientation and make it a constant to portrait/landscape, the changes in orientation of the device (phone) will not force the activity to be re-created. so even though you have separate layout for fragment it will not be loaded,
and if you make orientation of activity depending on the orientation change to the device (Phone) it will re-create the activity due to which you activity will initialize everything as result you are getting the first fragment while rotating the screen.
to deal with it, what you can do is use savedInstanceState(Bundle) while recreating the activity,
save the current fragment you have loaded and mention that to your savedInstanceState(Bundle) and after re-creating the activity when the orientation is changed, read the savedInstanceState and you will know which fragment to load
Please remove andorid:configChanges from manifest. Its purpose is that you yourself is handling configuration changes. Remove it and android will automatically pick the right layout file.
Hope this helps.
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 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'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.