4

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 Answer 1

25

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

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

if (display_mode == Configuration.ORIENTATION_PORTRAIT) {
    setContentView(R.layout.main);
} else {
    setContentView(R.layout.main_land);
}                           
4
  • I was looking in Activity for that getConfiguration() method... Android why must you be so obtuse. Thank you ShadowGod!
    – Alex Wayne
    Aug 28, 2010 at 4:58
  • 13
    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
    Aug 28, 2010 at 5:12
  • Ahhh thanks Chris as a beginner I didn't realize that thank you.
    – ShadowGod
    Aug 28, 2010 at 5:21
  • 2
    You can do this for a lot of things - docs here: developer.android.com/guide/topics/resources/…
    – QRohlf
    Aug 28, 2010 at 5:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.