Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Android-XML:Placing a <WebView> in a parent element that uses a wrap_content size can lead to subtle bugs; use match_parent

I am a beginner in Android and building a linear layout and getting an error in the layout XML file like this,

Error

 Placing a <WebView> in a parent element that uses a wrap_content size can lead to subtle bugs; use match_parent

Error is shown in this part of the code

<WebView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/browse"
        />

Here is my full code of the XML file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:weightSum="100" >

        <EditText
            android:id="@+id/url"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="70"
            android:ems="10" >
        </EditText>

        <Button
            android:id="@+id/go"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="30"
            android:text="GO" />
    </LinearLayout>

    <LinearLayout
         android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
         android:weightSum="8"

        >
    <Button
        android:id="@+id/backpage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="Go Back" />

    <Button
        android:id="@+id/forwardpage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="Forward Page" />

    <Button
        android:id="@+id/Refreshpage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="Refresh Page" />

    <Button
        android:id="@+id/clearhistory"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="Clear History" />

</LinearLayout>

    <WebView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/browse"
        />


</LinearLayout>

Can anyone tell me what is wrong in my code and how can I get rid of the error ?

I think this might be a very basic question but I tried and could not figure it out.

Details:

API Level : API 19: Android 4.2.2

like image 303
Code Geek Avatar asked Nov 03 '14 07:11

Code Geek


2 Answers

Contrary to what most answers imply, this is not a bug in Eclipse resp. Android Studio, but a fair warning. It's produced by a LINT check on your layout.

You can remove the warning and work around the 'subtile bugs' like so:

  1. Add tools:ignore="WebViewLayout" to your WebView (thanks to @StefanDeitmar) and make the tools namespace known by adding xmlns:tools="http://schemas.android.com/tools" to your outmost layout element.
  2. In your code, add a WebViewClient to your WebView and implement the OnPageFinished() callback to invoke a requestLayout() on your wrapping layout element.
mWebView.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView webView, String url) {
        super.onPageFinished(webView, url);
        mSurroundingLayout.requestLayout();
    }
}
like image 198
Ridcully Avatar answered Sep 22 '22 15:09

Ridcully


The issue is mainly because in the parent LinearLayout, you have provide layout_width and layout_height as wrap_content. It should be match_parent.

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    android:orientation="vertical" >
like image 36
turtle Avatar answered Sep 24 '22 15:09

turtle