4

I added android:configChanges="orientation" and I override onConfigurationChanged(). What I'd like to do is allow the landscape orientation change only if the device has a tablet-sized display resolution setting (shorter side greater than 480px - suggestions for better way of handling this are welcome!). I was thinking that this would work one of two ways, but neither is working in my tests either on my phone or in the emulator.

My first idea was to not call the super class' implementation of onConfigurationChanged(), but I get an exception thrown that complains about me not calling the super class.

My second idea was to just manually set newConfig.orientation to Configuration.ORIENTATION_PORTRAIT before passing it to the super class. In between my Activity and the actual Activity class in the framework I have an ActivityBase class that I use where I am overriding this as well. I can toast the value of newConfig.orientation in ActivityBase to see that the configuration object indeed being passed around does have the new value, but it is ignored and the orientation change has already occurred.

Where can I put code that allows me to conditionally short circuit the orientation change in some cases and allow it in others?

Reno
  • 33,594
  • 11
  • 89
  • 102
Rich
  • 36,270
  • 31
  • 115
  • 154

1 Answers1

2

I would set the requested orientation to fix in the Activity onCreate() method, something like this:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

if the Display isn't a xhdmi Display or has the required resolution.

HefferWolf
  • 3,894
  • 1
  • 23
  • 29
  • I'm assuming this is the equivalent of using orientation="portrait" in my xml. So, when I don't call this line of code, I'm using the orientation that the device gives me? Sounds good...gonna try it out now. Btw...any insight on properties of the display to look at? I was thinking px > 480 on shorter side...is there a property or method that will more succinctly tell me "extra large screen"? – Rich Mar 02 '11 at 15:53
  • Yes, this is the same as setting the property in the xml. For the display issue use the DisplayMetris:DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); And the density defined there, DENSITY_XHIGH for xhdmi. – HefferWolf Mar 02 '11 at 15:59
  • Rather than testing for pixel density (which could be any size), would testing for `if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == SCREENLAYOUT_SIZE_XLARGE)` work (if you're working with at least API Level 9, where xlarge was added)? – erichamion Mar 02 '11 at 17:06