简体   繁体   中英

Android: webview doesn't scroll enough when keyboard opens

In my app I have a webview which loads a webpage where the user should perform authentication. When the user selects some input field in the webpage, the webview should:

  1. focus on the field
  2. open the keyboard
  3. perform a scroll up in order for the user to keep seeing the field

However, the webview doesn't scroll up automatically, so the user doesn't see the field anymore. If the user tries manually to scroll, the webview does only a small scroll - not enough for the user to see everything he needs. The problem in not with the webpage itself, since when I browse to this webpage using android chrome, it does scroll up to keep the field in view and allows scrolling till the bottom of the page. I've read the following questions: WebView doesn't scroll when keyboard opened and adjustPan not preventing keyboard from covering EditText but the answers didn't fix the issue.

My current activity_web_viewlogin.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/portalWebViewWrapper"
    android:orientation="vertical"
    tools:context="com.hpe.sb.mobile.app.features.login.activities.WebViewLoginActivity">
    <include
        android:id="@+id/app_bar"
        layout="@layout/app_bar" />

   <ScrollView android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:fillViewport="true">

        <WebView
            android:id="@+id/portalWebView"
            android:layout_below="@id/app_bar"
            android:layout_width="match_parent"
            android:layout_margin="@dimen/activity_vertical_margin"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

   </ScrollView>
</LinearLayout>

Tried also that the ScrollView contains the LinearLayout.

In my AndroidManifest.xml:

        <activity
           android:name=".features.login.activities.WebViewLoginActivity"
           android:windowSoftInputMode="adjustResize"
           android:label="" />

Tried also adjustPan instead of adjustResize.

Thanks in advance!

Thanks to a friend, I now have a solution.

In AndroidManifest.xml:

<activity
            android:name=".features.login.activities.WebViewLoginActivity"
            android:label=""
            android:windowSoftInputMode="adjustResize"
            android:theme="@style/webview"/>

In styles.xml:

<style name="webview" parent="AppTheme.NoActionBar">
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowActionBar">false</item>
        <item name="android:windowFullscreen">false</item>
    </style>

This is activity_web_viewlogin.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:id="@+id/portalWebViewWrapper"
    tools:context="com.hpe.sb.mobile.app.features.login.activities.WebViewLoginActivity">
    <include
        android:id="@+id/app_bar"
        layout="@layout/app_bar" />
    <WebView
        android:id="@+id/portalWebView"
        android:layout_below="@id/app_bar"
        android:layout_width="match_parent"
        android:layout_margin="@dimen/activity_vertical_margin"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

Nothing special about the activity layout. It does work without ScrollView like @ramji explained. I believe it will also work with LinearLayout instead of RelativeLayout, didn't matter to me.

Webview is already comes with scrollable no need to use inside scrollview Replace your code with:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/portalWebViewWrapper"
    android:orientation="vertical"
 tools:context="com.hpe.sb.mobile.app.features.login.activities.WebViewLoginActivity">
    <include
        android:id="@+id/app_bar"
        layout="@layout/app_bar" />

        <WebView
            android:id="@+id/portalWebView"
            android:layout_below="@id/app_bar"
            android:layout_width="match_parent"
            android:layout_margin="@dimen/activity_vertical_margin"
            android:layout_height="match_parent"/>

</LinearLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM