0

Let's say I define two layouts with the same name, and place one of them in "layout-land" directory.

If the two layout files contain the same Views (and views ids), then my fragment/activity will find them and there will be no problem there.

However, I'm facing a situation where portrait and landscape designs are really different and I need to change the custom view / widget types implemented in each layout file.

If views ids or view types are different, or not found, it could lead to NullPointer exceptions or ClassCast exceptions.

I don't like the idea of checking types or nulls in my fragment code, and I thought there must be a better way to do this but I haven't found it yet.

Does anyone know a "cool" or at least more elegant way to achieve this?

Thanks !

agustinaliagac
  • 829
  • 10
  • 24

1 Answers1

0

In your layout folder, inside of resources, you need to have two separate layout files, one for portrait, and one for landscape.

Example:

- res
- - layout
- - - main_layout
- - - main_layout-land

There you have it, your app will now use two different layout files, depending on it's orientation. See This stackoverflow for more info.

Community
  • 1
  • 1
Josh Beckwith
  • 1,432
  • 3
  • 20
  • 38
  • Hello Josh, please read my question description. I know about layout resource handling, I need to know how to manage the situation where views and widgets are totally different between layout files. – agustinaliagac Nov 18 '16 at 19:55
  • Since Android creates a new actiity entirely when you change device state, you could possibly listen for a change in device orientation and launch a new activty. http://stackoverflow.com/questions/5726657/how-to-detect-orientation-change-in-layout-in-android see this thread. Depending on the orientation, you can launch an entire new activity or replace the fragment with the appropriate one. This should help get rid of your issue. – Josh Beckwith Nov 18 '16 at 21:02