views:

46

answers:

2

I want my view to change when it has changed to horizontal orientation:

@Override
    public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
      setContentView(R.layout.chartsactivity);
    }

This works, but I want my layout to switch back when it is in portrait mode.

A: 

Just look at the orientation of newConfig and use a different layout in each case? It would be much better if you let the system do it for you though.

Romain Guy
how can I let the system do it for me?
Sheehan Alam
A: 

To let the system handle the layout change you have to specify a special layout for landscape mode for the layout files you want to look different in landscape mode. This is done through a special folder for landscape layouts. Just put a layout-land folder in your resources folder. If your application is running in landscape mode, and such a folder is present the system will look for layout files in this folder first and if the layout is not found fall back to the standard layout folder.

Janusz
But where in my code do I map the layout objects (say its an ImageView)? I want it to download an image from a URL when it switches into landscape. I need to be able to add some logic here...
Sheehan Alam
Also how will it know which standard layout to fall back to when I go back into portrait mode, if I have more than 1 layout in my layout folder?
Sheehan Alam
I placed a layout in the folder called layout-land in my resources folder. Putting my phone in landscape orientation did not switch to this layout.
Sheehan Alam
if you have a layout called main_screen and your phone is in landscape mode then the system will look in the layout-land folder for a file called main_screen. After switching back the system will look in the standard folder for a file called main_screen. Have a look at http://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources for more details
Janusz