View Full Version : Handling orientation change (android)

11-09-2011, 04:39 PM
I'm trying to implement orientation changing while in an app and it's not going so well. For a while it was actually easier than I expected, the 3d stuff isn't hard to do (makes sense, only the camera needs to be changed) but the 2d GUI stuff is a different matter.
Probably the biggest thing is the screensize doesn't change even though OnScreenSizeChange() is triggered and that's what I use to handle everything. If you start up in portrait, the screen size will be eg 480*772 (28 missing pixels are for the android status bar), then when you change to landscape, it will still be 480*772 instead of 800*452. If you start up in landscape, resolution is 800*452 and stays that way even in portrait mode. All this time OnScreenSizeChange triggers as expected runs the code I put in to patch things. I've been trying to work with this, recreating 2d elements with the new parameters, and have been able to get acceptable results in the way graphics are scaled, but I just noticed touch coordinates are completely wrong so I finally decided to post here... I'd like for this to be a bit more straight forward :)

11-15-2011, 05:16 AM
Hi, sorry for the slow reply. My own games haven't required handling orientation change during gameplay (locked in landscape) so I'm not surprised there are problems with that. Sorry about that.

It's tricky working with those screen things, and really requires a lot of LogMsg()'s to figure out what's happening. I suspect I'm not even sending orientation change info from the Java side to the C++ side, that's the first thing to check.

Anyway, if you do get it working, please send a patch...

11-15-2011, 03:50 PM
If you're not doing orientation change, Android does and it triggers the screen resize stuff that you have set up. AppResize in AndroidUtils.cpp would handle the screensize change, but you've set it behind an if (!GetBaseApp()->IsInitted()) check so it only runs once when starting up. I took the line SetupScreenInfo out of that check, and changed it to use the new w and h values instead of GetScreenSize() calls. It also takes an orientation variable but I don't think it is used and/or necessary. (ofcourse you do need to know what the orientation is to make changes to your interface but knowing the resolution is enough)
Anyway after doing this it just works. Maybe this does surprise you :)

I don't know how to submit a patch, and there's a ton of other changes/additions I made that you don't want. It's just one line to change though.

edit: just found an issue, changing orientation while the screen is off does not send the change to the app. Can probably fix that in OnEnterForeground.

edit: one more thing. EntitySetScaleBySize behaves rather oddly if you do it more than once. Not a big deal (I can just recreate the entity) but you might want to know.

01-02-2012, 01:37 AM
(just noticed this, so replying a bit late..)

Thanks! I'll make the change you suggested the next time I'm doing some android builds!