Search code examples
androidcocos2d-x

Cocos2dx Landscape and Portrait on Android Devices


Hi I am currently Learning cocos2dx. I am building this game on a portrait view. the problem is the image is so small. Why?

landscape

portrait

Also how can I make it hide the navigation bar?


Solution

  • For portrait in AndroidManifest.xml change screen orientation to portrait:

     <activity android:name="org.cocos2dx.cpp.AppActivity"
                      android:label="@string/app_name"
                      android:screenOrientation="portrait"
                      android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
                      android:configChanges="orientation">
    

    Also set proper design resolution in AppDelegate.cpp (when it was 960x640 for example now it should be 640x960 for potrait).

    As for hiding navigation bar you have to change target sdk level to api level 19 (kitkat) and then change AppActivity class implementation for the following:

    public class AppActivity extends Cocos2dxActivity {
        private Cocos2dxGLSurfaceView glSurfaceView;
    
        public Cocos2dxGLSurfaceView onCreateView()
        {
            glSurfaceView = new Cocos2dxGLSurfaceView(this);
            this.hideSystemUI();
            glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);
    
            return glSurfaceView;
        }
    
        public void onWindowFocusChanged(boolean hasFocus)
        {
            super.onWindowFocusChanged(hasFocus);
            if (hasFocus)
            {
                this.hideSystemUI();
            }
        }
    
    @TargetApi(Build.VERSION_CODES.KITKAT)
        private void hideSystemUI()
        {
            if (Build.VERSION.SDK_INT >= 19) {
                glSurfaceView.setSystemUiVisibility(
                        Cocos2dxGLSurfaceView.SYSTEM_UI_FLAG_LAYOUT_STABLE 
                        | Cocos2dxGLSurfaceView.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | Cocos2dxGLSurfaceView.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | Cocos2dxGLSurfaceView.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | Cocos2dxGLSurfaceView.SYSTEM_UI_FLAG_FULLSCREEN
                        | Cocos2dxGLSurfaceView.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
            }
        }
    }
    

    This will only work for android 4.4+ devices and on the older just won't do anything.