tags:

views:

25

answers:

1

My Activity needs to monitor the orientation of the device. Now this works great with onConfigurationChanged(), but I also need to know orientation when my Activity starts.

So how do find out the current orientation of the device in my onCreate(), for instance?

+1  A: 

I'm no expert but this works for me, in onCreate():

int display_mode = getResources().getConfiguration().orientation;

if (display_mode == 1) {
    setContentView(R.layout.main);
} else {
    setContentView(R.layout.main_land);
}                           
ShadowGod
I was looking in `Activity` for that `getConfiguration()` method... Android why must you be so obtuse. Thank you ShadowGod!
Squeegy
There is no reason for you to set your views this way. Android is build auto-magicly to use the right ones based on their folder locations. You can place your default layout /res/layout/ and the landscape one in /res/layout-land with the same name. During onCreate Android detects the layout and uses the correct resource. The same may be done with screen density and other things.
smith324
Ahhh thanks Chris as a beginner I didn't realize that thank you.
ShadowGod
No problem, glad I could help.
smith324
You can do this for a lot of things - docs here: http://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources
QRohlf