@@ -77,6 +77,8 @@ public class MainActivity extends AppCompatActivity {
77
77
78
78
private String location = "" ;
79
79
80
+ private int currentZoom ;
81
+
80
82
// This is for the file picker dialog invoked by file upload forms in the WebView.
81
83
// Used by e.g. MoonPay's KYC forms.
82
84
private ValueCallback <Uri []> filePathCallback ;
@@ -221,12 +223,37 @@ public void run() {
221
223
// For MoonPay WebRTC camera access.
222
224
vw .getSettings ().setMediaPlaybackRequiresUserGesture (false );
223
225
226
+ // Retrieve the current text zoom setting to adjust the base font size in the WebView.
227
+ currentZoom = vw .getSettings ().getTextZoom ();
228
+
224
229
vw .setWebViewClient (new WebViewClient () {
225
230
@ Override
226
231
public void onPageFinished (WebView view , String url ) {
227
232
// The url is not correctly updated when navigating to a new page. This allows to
228
233
// know the current location and to block external requests on that base.
229
234
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
+
230
257
}
231
258
@ Override
232
259
public WebResourceResponse shouldInterceptRequest (final WebView view , WebResourceRequest request ) {
0 commit comments