From eae8ffd2b09a6e05e078bb55387a7905ed3b6edb Mon Sep 17 00:00:00 2001 From: Der_Googler <54764558+DerGoogler@users.noreply.github.com> Date: Sat, 17 Aug 2024 12:06:22 +0200 Subject: [PATCH] fix keyboard is overdraw elements (android) --- .../com/dergoogler/mmrl/MainActivity.java | 48 ++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/dergoogler/mmrl/MainActivity.java b/app/src/main/java/com/dergoogler/mmrl/MainActivity.java index d4cf2e1a..c6949bff 100644 --- a/app/src/main/java/com/dergoogler/mmrl/MainActivity.java +++ b/app/src/main/java/com/dergoogler/mmrl/MainActivity.java @@ -2,13 +2,18 @@ import android.annotation.SuppressLint; import android.graphics.Color; +import android.graphics.Rect; import android.os.Build; import android.os.Bundle; import android.os.StrictMode; import android.util.Log; +import android.view.View; +import android.view.ViewTreeObserver; import android.webkit.ConsoleMessage; import android.webkit.WebSettings; import android.webkit.WebView; +import android.widget.FrameLayout; +import android.widget.LinearLayout; import androidx.core.view.WindowCompat; @@ -27,6 +32,12 @@ import org.apache.cordova.engine.SystemWebViewEngine; public class MainActivity extends CordovaActivity { + + private WebView wv; + private View rootView; + private int previousHeight = 0; + private boolean isKeyboardShowing = false; + @Override @SuppressLint("SetJavaScriptEnabled") public void onCreate(Bundle savedInstanceState) { @@ -40,10 +51,33 @@ public void onCreate(Bundle savedInstanceState) { StrictMode.setThreadPolicy(policy); } - WebView wv = (WebView) appView.getEngine().getView(); + wv = (WebView) appView.getEngine().getView(); + rootView = findViewById(android.R.id.content); CordovaWebViewEngine wve = appView.getEngine(); + rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { + @Override + public void onGlobalLayout() { + Rect r = new Rect(); + rootView.getWindowVisibleDisplayFrame(r); + int screenHeight = rootView.getRootView().getHeight(); + int keypadHeight = screenHeight - r.bottom; + + if (keypadHeight > screenHeight * 0.15) { // 0.15 ratio is perhaps enough to determine keypad height. + if (!isKeyboardShowing) { + isKeyboardShowing = true; + adjustWebViewHeight(keypadHeight); + } + } else { + if (isKeyboardShowing) { + isKeyboardShowing = false; + resetWebViewHeight(); + } + } + } + }); + NativeStorage ns = new NativeStorage(this); NativeOS os = new NativeOS(this); this.applyTheme(wv, ns, os); @@ -98,6 +132,18 @@ public boolean onConsoleMessage(ConsoleMessage consoleMessage) { }); } + private void adjustWebViewHeight(int keypadHeight) { + FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) wv.getLayoutParams(); + params.height = rootView.getHeight() - keypadHeight; + wv.setLayoutParams(params); + } + + private void resetWebViewHeight() { + FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) wv.getLayoutParams(); + params.height = LinearLayout.LayoutParams.MATCH_PARENT; + wv.setLayoutParams(params); + } + private String mmrlUserAgent() { return "MMRL/" + BuildConfig.VERSION_NAME + " (Linux; Android " + Build.VERSION.RELEASE + "; " + Build.MODEL + " Build/" + Build.DISPLAY + ")"; }