diff --git a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java index 50d961f2a6..e2816d82b1 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java +++ b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java @@ -447,7 +447,10 @@ private void initWebView() { Logger.debug("WebView background color not applied"); } - webView.requestFocusFromTouch(); + if (config.isInitialFocus()) { + webView.requestFocusFromTouch(); + } + WebView.setWebContentsDebuggingEnabled(this.config.isWebContentsDebuggingEnabled()); } diff --git a/android/capacitor/src/main/java/com/getcapacitor/CapConfig.java b/android/capacitor/src/main/java/com/getcapacitor/CapConfig.java index 2e44503342..22f3db1a3c 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/CapConfig.java +++ b/android/capacitor/src/main/java/com/getcapacitor/CapConfig.java @@ -40,6 +40,7 @@ public class CapConfig { private boolean captureInput = false; private boolean webContentsDebuggingEnabled = false; private boolean loggingEnabled = true; + private boolean initialFocus = true; // Embedded private String startPath; @@ -116,6 +117,7 @@ private CapConfig(Builder builder) { this.captureInput = builder.captureInput; this.webContentsDebuggingEnabled = builder.webContentsDebuggingEnabled; this.loggingEnabled = builder.loggingEnabled; + this.initialFocus = builder.initialFocus; // Embedded this.startPath = builder.startPath; @@ -187,6 +189,8 @@ private void deserializeConfig(@Nullable Context context) { loggingEnabled = isDebug; } + initialFocus = JSONUtils.getBoolean(configJSON, "android.initialFocus", initialFocus); + // Plugins pluginsConfiguration = deserializePluginsConfig(JSONUtils.getObject(configJSON, "plugins")); } @@ -243,6 +247,10 @@ public boolean isLoggingEnabled() { return loggingEnabled; } + public boolean isInitialFocus() { + return initialFocus; + } + public PluginConfig getPluginConfiguration(String pluginId) { PluginConfig pluginConfig = pluginsConfiguration.get(pluginId); if (pluginConfig == null) { @@ -398,6 +406,7 @@ public static class Builder { private boolean captureInput = false; private Boolean webContentsDebuggingEnabled = null; private boolean loggingEnabled = true; + private boolean initialFocus = false; // Embedded private String startPath = null; @@ -496,5 +505,10 @@ public Builder setLoggingEnabled(boolean enabled) { this.loggingEnabled = enabled; return this; } + + public Builder setInitialFocus(boolean focus) { + this.initialFocus = focus; + return this; + } } } diff --git a/cli/src/declarations.ts b/cli/src/declarations.ts index dc17d5befb..0c12bbb761 100644 --- a/cli/src/declarations.ts +++ b/cli/src/declarations.ts @@ -203,6 +203,14 @@ export interface CapacitorConfig { * @since 3.1.0 */ flavor?: string; + + /** + * Whether to give the webview initial focus. + * + * @since 3.5.1 + * @default true + */ + initialFocus?: boolean; }; ios?: {