Skip to content

Commit df796db

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 5aeeb8b commit df796db

File tree

1 file changed

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

1 file changed

+21
-0
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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,31 @@ 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+
// Reset the WebView's text zoom to 100% to ensure that the scaling is controlled via CSS
245+
// and not by the WebView's default scaling behavior.
246+
view.getSettings().setTextZoom(100);
247+
248+
// Execute the CSS setup in the WebView.
249+
view.evaluateJavascript(cssSetup, null);
250+
230251
}
231252
@Override
232253
public WebResourceResponse shouldInterceptRequest(final WebView view, WebResourceRequest request) {

0 commit comments

Comments
 (0)