4

I'm having trouble building a layout. My situation is this:

I have an Activity that runs and has a layout defined for both normal and landscape, but they are built in runtime from onCreate().

In onCreate() I make the call to setContentView(my_normal_layout) which is then added to using a lot of addView()'s etc and works fine. However I am also trying to build my landscape layout but have the problem that when I try and use findViewById() to get my base layout which is part of my layout file stored in res/layout-land it returns null.

Now I've looked at solutions for people having the null return problem but people are suggesting that setContentView must be executed first. However I don't want to set the content view to be my landscape layout, I only want that to happen when the screen is rotated (it does this automatically?).

So does anyone know how I can get around this problem of using findViewById and altering my view that isn't set as the content view?

I can post any lines of code that are required but I'm hoping that explanation is good enough. Thanks,

Josh

Heiko Rupp
  • 30,426
  • 13
  • 82
  • 119
Josh Suckling
  • 217
  • 7
  • 14
  • see this: http://stackoverflow.com/a/4858040/1010868 – Tomasz Gawel Apr 23 '12 at 23:19
  • and this: http://developer.android.com/guide/practices/screens_support.html#qualifiers :) – Tomasz Gawel Apr 23 '12 at 23:20
  • I understand this already but it cant solve my problem becase I need a seperate layout for both. – Josh Suckling Apr 23 '12 at 23:23
  • 1
    put portrait layout into `/res/layout/my_layout.xml` and landscape into `/res/layout-land/my_layout.xml` thay can be completely different files - if some viewid is missing in one of them then you'll simply get null from findViewById which you can adequately. – Tomasz Gawel Apr 23 '12 at 23:32

1 Answers1

4

If I understand correctly, you have different UI elements in the two different layout files.

If that is the case then you can still name the layout files the same (one in /res/layout and one in /res/layout-land) and use setContentView(...) to automatically inflate the correct one.

After that, all you need to know is what the current orientation is (portrait or landscape) in order to know which bits of code to call with findViewById(...).

You can find the orientation using...

getWindowManager().getDefaultDisplay().getOrientation();

If you are using API 8 (Android v2.2) or later, the above is deprecated however and you should call getRotation() instead of getOrientation().

See Display.getOrientation()

Squonk
  • 48,735
  • 19
  • 103
  • 135
  • Ideally, both of your layout files (though completely different will still have all of the same view elements so that the null findViewById() isn't an issue, but this is by far the best approach – JRaymond Apr 23 '12 at 23:35
  • @JRaymond: Agreed. The usual case would be to simply rearrange the same UI elements to make the best use of the orientation and use the same resource ids. There may be a legitimate reason to add extra elements to the landscape layout depending on the OP's app although it's not something I've ever needed to do. – Squonk Apr 23 '12 at 23:45
  • Cheers for the help, ive used the method of getting the orientation to set my correct context view. Thanks for the extra advice @JRaymond also. Im pretty new to android development and so im still experimenting different things. The reason why I had 2 layouts was because when the phone is portrait im showing a timetable as a list of entries but then in landscape im showing a grid of the timetable. Im nearly there to having it fully functional as I require but I can happily admit that its a bit messy and no doubt there are much better ways to do things. Trial and error. Cheers again. – Josh Suckling Apr 24 '12 at 00:07
  • @JoshSuckling: Glad to help. I can see the logic in the list and grid views now you've explained it. – Squonk Apr 24 '12 at 00:20