@@ -77,6 +77,8 @@ public class MainActivity extends AppCompatActivity {
7777
7878 private String location = "" ;
7979
80+ private int currentZoom ;
81+
8082 // This is for the file picker dialog invoked by file upload forms in the WebView.
8183 // Used by e.g. MoonPay's KYC forms.
8284 private ValueCallback <Uri []> filePathCallback ;
@@ -221,12 +223,37 @@ public void run() {
221223 // For MoonPay WebRTC camera access.
222224 vw .getSettings ().setMediaPlaybackRequiresUserGesture (false );
223225
226+ // Retrieve the current text zoom setting to adjust the base font size in the WebView.
227+ currentZoom = vw .getSettings ().getTextZoom ();
228+
224229 vw .setWebViewClient (new WebViewClient () {
225230 @ Override
226231 public void onPageFinished (WebView view , String url ) {
227232 // The url is not correctly updated when navigating to a new page. This allows to
228233 // know the current location and to block external requests on that base.
229234 view .evaluateJavascript ("window.location.pathname" , path -> location = path );
235+
236+ // Calculate the base font size for html as a percentage.
237+ // This percentage dynamically adjusts to ensure 1rem = 10px, scaled according to the current zoom level.
238+ double baseFontSizePercentage = 62.5 * (currentZoom / 100.0 );
239+
240+ // The default body font size in rem, which is independent of the zoom level.
241+ // This size does not scale dynamically with zoom adjustments and is fixed at 1.6rem.
242+ double defaultFontSizeREM = 1.6 ;
243+
244+ // Set the base font-size on the html element dynamically and apply a fixed font size to the body.
245+ // Also, set a custom data attribute 'data-test' to the calculated base font size percentage for potential debugging.
246+ String cssSetup = "document.documentElement.style.fontSize = '" + baseFontSizePercentage + "%';" +
247+ "document.body.style.fontSize = '" + defaultFontSizeREM + "rem';" +
248+ "document.body.setAttribute('data-test', '" + baseFontSizePercentage + "%');" ;
249+
250+ // Reset the WebView's text zoom to 100% to ensure that the scaling is controlled via CSS
251+ // and not by the WebView's default scaling behavior.
252+ view .getSettings ().setTextZoom (100 );
253+
254+ // Execute the CSS setup in the WebView.
255+ view .evaluateJavascript (cssSetup , null );
256+
230257 }
231258 @ Override
232259 public WebResourceResponse shouldInterceptRequest (final WebView view , WebResourceRequest request ) {
0 commit comments