How to handle runtime resolution change on Android

How to handle runtime resolution change on Android
0

Hey, I’m trying to handle switching resolutions from Settings on Android while the game is running and after some experimenting I haven’t found out how to do that.

So you can see what I’m trying to do, I built the CppEmptyTest from the Cocos repo. When I run the game, it looks as expected (the black stripe on the left is just because of the screen cutout, don’t worry about that):

Then, while the game is running, I go to settings and change the screen resolution from 1520x720 to 3040x1440:

When I go back to the game, the result is this:

If I kill the app and start it again, it works fine, so it’s not a problem of handling multiple screen sizes, that works as expected. But how can I handle the resolution changing while the game is running?

There are some callbacks where I can detect the change but I don’t know how to handle it. Probably the GL surface needs to be recreated or at least the scale factor needs to be updated, but I’m not sure how to do that and several things I’ve tried either end up crashing the app or have no impact at all.

Is anyone handling this properly in their games? Any help is appreciated. Thanks!

Can you check the LayoutParams in your Cocos2dxActivity.java… are you familiar with Android UI?

Definitely cocos will not handle this for you.
Search forum for resolution change while going to portrait to landscape. Same thing will help you to achieve this as well.

Nope, never done this. May i know why you want to do this? Because i think its on user if he/she changes such config from device settings then user must restart game for perfect user experience.

Search forum for resolution change while going to portrait to landscape. Same thing will help you to achieve this as well.

I’m not sure what you mean. There shouldn’t be any resolution change when switching from portrait to landscape, right? I’ve searched the forums quite a bit, but haven’t found anything like this. Do you maybe have any threads you can point me to? Or can you give me a general idea of what the solution involves?

May i know why you want to do this? Because i think its on user if he/she changes such config from device settings then user must restart game for perfect user experience.

Well, I don’t think the user should be required to kill the game, plus we’ve tried other games and they seem to be able to handle the resolution switch just fine. Granted, it’s not a huge priority, because this will almost never happen, but still if we can figure out how to handle this, it’d be nice to do it.

Can you check the LayoutParams in your Cocos2dxActivity.java … are you familiar with Android UI?

I’m familiar with Android UI, but I’m certainly not an expert. In Cocos2dxActivity.java mFrameLayout (the GLSurfaceView parent) is set like so:

ViewGroup.LayoutParams framelayout_params =
            new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                       ViewGroup.LayoutParams.MATCH_PARENT);

        mFrameLayout = new ResizeLayout(this);

        mFrameLayout.setLayoutParams(framelayout_params);

That looks ok to me. But since there’s a GL view involved, I’m not sure the scale works just like that. Do you have any suggestion?

Suppose you run game in full hd resolution device i.e. 1920 x 1080
Then for portrait game resolution will be 1080 w x 1920 h and for landscape 1920 w x 1080 h.
so when orientation change you need to update glview. I hope you get it.

well see here :slight_smile:


Also here

Cool.
Let me know if this solve your problem or not.

1 Like