Skip to content

Commit e8ddfec

Browse files
committed
frontend/android: capture textzoom and set custom base font size
Android textZoom only increases font-size without incresing surrounding elements which can break the layout and cause overlapping text and elements. This commit reads the current textzoom, sets a custom CSS base font size (calculated from textzoom) and resets textzoom to 100%. The custom CSS is using 'The 62.5% Font Size Trick' with which 1rem equals 10px. With this it is possible to change elment dimensions from px to rem. These elements will become responsive and also grow if Android settings for font size is increased. To simulate this in webdev and optimize the layout for increased fontsize a base font size of 125% has to be set.
1 parent 8ee1ab1 commit e8ddfec

File tree

1 file changed

+29
-3
lines changed
  • frontends/android/BitBoxApp/app/src/main/java/ch/shiftcrypto/bitboxapp

1 file changed

+29
-3
lines changed

frontends/android/BitBoxApp/app/src/main/java/ch/shiftcrypto/bitboxapp/MainActivity.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ public void onReceive(Context context, Intent intent) {
136136
}
137137
};
138138

139-
140139
@Override
141140
public void onConfigurationChanged(Configuration newConfig) {
142141
int currentNightMode = newConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK;
@@ -171,6 +170,8 @@ public void setDarkTheme(boolean isDark) {
171170
}
172171
}
173172

173+
private int currentZoom;
174+
174175
@SuppressLint("SetJavaScriptEnabled")
175176
@Override
176177
protected void onCreate(Bundle savedInstanceState) {
@@ -226,7 +227,33 @@ protected void onCreate(Bundle savedInstanceState) {
226227

227228
// vw.setWebContentsDebuggingEnabled(true); // enable remote debugging in chrome://inspect/#devices
228229

229-
vw.setWebViewClient(new BitBoxWebViewClient(BASE_URL, getAssets(), getApplication()));
230+
// Retrieve the current text zoom setting to adjust the base font size in the WebView.
231+
currentZoom = vw.getSettings().getTextZoom();
232+
233+
vw.setWebViewClient(new BitBoxWebViewClient(BASE_URL, getAssets(), getApplication()) {
234+
@Override
235+
public void onPageFinished(WebView view, String url) {
236+
super.onPageFinished(view, url);
237+
238+
// Calculate the base font size for html as a percentage.
239+
// This percentage dynamically adjusts to ensure 1rem = 10px, scaled according to the current zoom level.
240+
double baseFontSizePercentage = 62.5 * ((double) currentZoom / 100.0);
241+
242+
// The default body font size in rem, which is independent of the zoom level.
243+
// This size does not scale dynamically with zoom adjustments and is fixed at 1.6rem.
244+
double defaultFontSizeREM = 1.6;
245+
246+
// Reset the WebView's text zoom to 100% to ensure that the scaling is controlled via CSS
247+
// and not by the WebView's default scaling behavior.
248+
view.getSettings().setTextZoom(100);
249+
250+
String cssSetup = "document.documentElement.style.fontSize='" + baseFontSizePercentage + "%';" +
251+
"document.body.style.fontSize='" + defaultFontSizeREM + "rem';";
252+
253+
// Execute the CSS setup in the WebView.
254+
view.evaluateJavascript(cssSetup, null);
255+
}
256+
});
230257

231258
ActivityResultLauncher<String> fileChooser = registerForActivityResult(new ActivityResultContracts.GetContent(), uri -> webChrome.onFilePickerResult(uri));
232259
BitBoxWebChromeClient.CameraPermissionDelegate cameraPermissionDelegate = () -> ActivityCompat.requestPermissions(
@@ -321,7 +348,6 @@ public void noAuthConfigured() {
321348
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
322349
}
323350
}));
324-
325351
}
326352

327353
@Override

0 commit comments

Comments
 (0)