Roman Nazarevych
Roman Nazarevych

Reputation: 7703

Android WebView content display issue

I have text loaded into WebView

String html = "<p>1</p>\n" +
                "<p>2</p>\n" +
                "<p>3</p>\n" +
                "<p>4</p>\n" +
                "<p>5</p>\n" +
                "<p>6</p>\n" +
                "<p>7</p>\n" +
                "<p>8</p>\n" +
                "<p>9</p>\n" +
                "<p>10</p>\n" +
                "<p>11</p>\n" +
                "<p>12</p>\n" +
                "<p>13</p>\n" +
                "<p>14</p>\n" +
                "<p>15</p>\n" +
                "<p>16</p>\n" +
                "<p>17</p>\n"+
                "<p>18</p>\n";
        questionTextView.loadDataWithBaseURL("file:///android_asset/", html, "text/html", "UTF-8", null);

when I have 17 paragraphs the everything is displayed ok but when I add 18 and more all text disapears and I just see scroll line.

This is WebView setting

if (Build.VERSION.SDK_INT >= 11) {
        questionTextView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
  final WebSettings webSettings = questionTextView.getSettings();
        //float fontSize = getResources().getDimension(R.dimen.practice_text_size);
        //webSettings.setDefaultFontSize((int) fontSize);
        webSettings.setDefaultTextEncodingName("utf-8");
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSettings.setBlockNetworkImage(true);
        webSettings.setGeolocationEnabled(false);
        webSettings.setNeedInitialFocus(false);

WebView xml

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <ImageView
        android:id="@+id/question_flag"
        android:layout_width="24dip"
        android:layout_height="24dip"
        android:layout_gravity="top|left"
        android:layout_marginLeft="4dp"
        android:layout_marginTop="4dp"
        android:scaleType="centerInside" />


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

        <LinearLayout
            android:id="@+id/question_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">

            <de.phase6.vtrainer.custom.FlowLayout
                android:id="@+id/question_images"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dip"
                android:layout_marginRight="10dip"
                android:orientation="horizontal"
                android:visibility="gone" />

            <WebView
                android:id="@+id/question_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:paddingLeft="8dp"
                android:paddingRight="8dp" />


            <LinearLayout
                android:id="@+id/question_sounds_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:gravity="center"
                android:orientation="horizontal"
                android:visibility="gone" />


        </LinearLayout>
    </ScrollView>

    <ImageButton
        android:id="@+id/btn_undo"
        style="@style/BorderlessButton"
        android:layout_width="48dip"
        android:layout_height="48dip"
        android:layout_gravity="bottom|start"
        android:longClickable="true"
        android:src="@drawable/back"
        android:visibility="invisible" />

    <ImageButton
        android:id="@+id/btn_send_back"
        style="@style/BorderlessButton"
        android:layout_width="48dip"
        android:layout_height="48dip"
        android:layout_gravity="bottom|end"
        android:longClickable="true"
        android:src="@drawable/send_back" />

</FrameLayout>

<View style="@style/DividerHorizontal" />

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom">

    <LinearLayout
        android:id="@+id/input_answer_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/light_grey_ddd"
        android:padding="@dimen/practice_input_padding"
        android:orientation="vertical"
        android:visibility="visible">

        <TextView
            android:id="@+id/user_answer_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/your_answer"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textAllCaps="true"
            android:textColor="@color/grey" />

        <EditText
            android:id="@+id/input_answer"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@color/transparent"
            android:focusableInTouchMode="true"
            android:focusable="true"
            android:ems="10"
            android:imeOptions="actionDone"
            android:singleLine="true"
            android:inputType="textNoSuggestions" />
    </LinearLayout>

    <Button
        android:id="@+id/btn_see_answer"
        style="@style/BorderlessButton"
        android:layout_width="match_parent"
        android:layout_height="48dip"
        android:textAllCaps="true"
        android:text="@string/show_answer" />

</FrameLayout>

Upvotes: 1

Views: 664

Answers (1)

Roman Nazarevych
Roman Nazarevych

Reputation: 7703

After investigation I discovered that this issue is reproducing when you put your WebView into ScrollView and set

if (Build.VERSION.SDK_INT >= 11) {
        webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}

And amount of the information on the screen is approximately two screens or more then WebView become blank.

To fix this do not set LayerType or set webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);

Upvotes: 1

Related Questions