8

In an Android webview, when a text/password field is touched, the keyboard appears but covers the field, forcing the user to scroll to see what they are typing.

Is there a way to solve this issue or a way to have an auto-focus in web view ? (as seen in iOS).

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dedalos.amb">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:usesCleartextTraffic="true"
        android:windowSoftInputMode="adjustPan"
        android:theme="@style/AppTheme">
        <activity android:name=".SplashScreen" android:theme="@style/SplashTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity">
        </activity>
    </application>

</manifest>

Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@color/splash_background"
    tools:context=".MainActivity">

    <WebView
        android:id="@+id/ambWebViewLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/splash_background" />

</LinearLayout>

I also paste styles and colors here:

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#008577</color>
    <color name="colorPrimaryDark">#00574B</color>
    <color name="colorAccent">#D81B60</color>

    <color name="splash_background">#e11020</color>
</resources>

styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">#e11020</item>
        <item name="colorPrimaryDark">#e11020</item>
        <item name="colorAccent">#e11020</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:windowTranslucentNavigation">true</item>

    </style>

    <!-- SplashTheme -->
    <style name="SplashTheme" parent="AppTheme">
        <item name="android:windowBackground">@drawable/splash_theme</item>
    </style>

</resources>
Stefano
  • 297
  • 2
  • 4
  • 15

7 Answers7

7

Try adding the following property:

android:fitsSystemWindows="true"

in the root LinearLayout of the .xml layout.

animeshk
  • 376
  • 1
  • 7
  • And navigation bar is colored too! I can't use `android:fitsSystemWindows="true"` as long as I don't understand how to make status bar and navigation bar transparent again – Stefano Dec 20 '19 at 07:45
  • You can use a CoordinatorLayout as your activity root view and then setFitsSystemWindows(boolean) will work. You can check the following post for a detailed explaination. [link](https://medium.com/androiddevelopers/why-would-i-want-to-fitssystemwindows-4e26d9ce1eec?) @Stefano. – animeshk Dec 20 '19 at 20:49
  • @Stefano Also you may try alternative solutions if you do not want to change your layout. They can be found in this answer [here](https://stackoverflow.com/questions/7026854/textbox-hidden-below-keyboard-in-android-webview) – animeshk Dec 20 '19 at 20:51
  • unfortunately my app crashes with CoordinatorLayout instead of LinearLayout :-/ – Stefano Dec 26 '19 at 09:57
3

Android keyboard hides when the app use full screen mode.

This issue can be resolved if you use

<item name="android:windowFullscreen">false</item> in your styles.

then use <activity android:name=".MainActivity" android:windowSoftInputMode="adjustResize">

in your manifest.

Nawin
  • 485
  • 8
  • 14
  • great solution on fullscreen this was not working. but when I disable the fullscreen this works without need to change on manifest. thank you – Mimouni Jan 02 '22 at 16:44
1

You might want to check this article: https://developer.android.com/training/keyboard-input/visibility

Maybe this will help you.

android:windowSoftInputMode="adjustResize"
Sebastian M
  • 629
  • 7
  • 17
  • Hi, thank you very much for helping! I tried to insert it in the .MainActivity and unfortunately it doesn't work :-( the keyboard appears and covers the text field – Stefano Dec 18 '19 at 15:32
1

I resolved this issue by following answers

android:fitsSystemWindows="true" in root LinearLayout @animeshk

<item name="android:windowFullscreen">false</item> in theme style @Nawin

In my case, it worked well without adding android:windowSoftInputMode="adjustResize"

Ashley
  • 31
  • 4
0

You can try this following property:

  • In manifest file where you register your activity :

    <activity android:name="yourActivity"
       android:windowSoftInputMode="stateHidden"/>
    
Nidhi Savaliya
  • 162
  • 2
  • 9
0

There are two Alternatives:

Alternative 1 :

For single line to focus then use this:

webView.requestFocus(View.FOCUS_DOWN);

Alternative 2 :

webView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_UP:
                view.requestFocusFromTouch();  
                break;
        }               
        return false;
    }

});

I hope it'll help you...!

Viral Patel
  • 1,296
  • 1
  • 11
  • 24
0

Try this one.

webView.getSettings().setJavaScriptEnabled(true);

webView.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url) {
        view.loadUrl("javascript:document.getElementsByName('[your view name without square brackets]')[0].focus();");
        super.onPageFinished(view, url);
    }
});

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
Rohit Mandiwal
  • 10,258
  • 5
  • 70
  • 83