Search code examples
androidcapacitor

Scaling factor on a Android WebView Application


I'm creating an Android application for TV devices using vue and Capacitor to wrap the application inside a native app.

My applicaton as been developed on my PC a 2K monitor and the production TV is 2K.

When I execute the application both on a virtual and an actual TV the resolution shrinks to a width of 960 ( 1920 / 2 ) because it apply a pixel delnsity of 2.

Is it possible to change this behaviour in my generated Android project or Capcitor?


Solution

  • This trick worked

    public class MainActivity extends BridgeActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState); // WebView created here
    
            bridge.getWebView().postDelayed(() -> {
                bridge.getWebView().evaluateJavascript(
                        "var meta = document.createElement('meta');" +
                                "meta.name = 'viewport';" +
                                "meta.content = 'width=1920, initial-scale=1.0, maximum-scale=0.5, user-scalable=no';" +
                                "document.getElementsByTagName('head')[0].appendChild(meta);",
                        null
                );
            }, 500);
    
            WebView webView = bridge.getWebView();
            WebSettings webSettings = webView.getSettings();
            webSettings.setUseWideViewPort(true);
            webSettings.setLoadWithOverviewMode(true);
            webSettings.setSupportZoom(false);
            webSettings.setBuiltInZoomControls(false);
        }
    }       
    

    MainActivity is the class generated by Capacitor