Skip to content

Commit

Permalink
fix keyboard is overdraw elements (android)
Browse files Browse the repository at this point in the history
  • Loading branch information
DerGoogler committed Aug 17, 2024
1 parent f81a414 commit eae8ffd
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion app/src/main/java/com/dergoogler/mmrl/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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 + ")";
}
Expand Down

0 comments on commit eae8ffd

Please sign in to comment.