14

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

2
  • 1
    in parent linear layout change android:layout_width="match_parent" android:layout_height="match_parent"
    – turtle
    Nov 3, 2014 at 10:36
  • 1
    i will add this as an answer so that other facing same issue can be benefited.
    – turtle
    Nov 3, 2014 at 12:25

4 Answers 4

14

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();
    }
}
1
  • Good answer; this worked for me. I believe the basic issue is that WebView can take a while to lay itself out (and for some pages the height will change often). So this tells the enclosing layouts that they should readjust based on the just-measured height. Unless, of course, there are other subtle bugs which I haven't yet noticed...
    – DFedor
    Jan 6, 2017 at 0:49
10

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" >
1
  • So changing the layout_width and layout_height to match_parent generates an error "Error: String types not allowed (at 'layout_height' with value 'match_parent')."
    – gnac
    Feb 20, 2015 at 7:17
6

Actually I think that this is a bug in Eclipse, or the latest version 20 Android SDK.

I have screen designs that are made up of stacked web views because their contents come from different sources. My screen layouts "wrap content" for height so that the user always sees what information is returned from the various sources without vast areas of blank. Its an unusual design, but one that has worked well for 4+ years on many similar applications. Yup, I know that I have to be careful about heights, etc, but I'm happy to take on that responsibility.

Eclipse is essentially creating a "Nanny State" - that's not the way we normally do it so its wrong. Pardon me but I differ in opinion.

The way I got around this was to close the XML file and completely clean the project. This way Eclipse forgets that it ever "nanny nagged" about this issue and everything is good again.

4
  • 4
    Or, you could just add tools:ignore="WebViewLayout" to your WebView and Android Lint would stop nagging you about it. Jan 27, 2015 at 11:23
  • I agree that this is likely a bug in Eclipse. I have an existing webview element that works fine for a couple of releases now. However, today, if I make a [meaningless] change to that layout Eclipse starts throwing this error. Making the change to match turtle's recommendation above doesn't work as Eclipse doesn't recognize "match_parent". I had to close and revert the file to get the errors to go away.
    – gnac
    Feb 20, 2015 at 7:32
  • Also note that I can make the same desired changes in an external editor and Eclipse doesn't mind and will build fine, although "match_parent" still causes the build to fail.
    – gnac
    Feb 20, 2015 at 7:40
  • 2
    I upvoted because the fix works, but this isn't a bug. It's a new lint rule. Sep 29, 2016 at 15:02
4

I had the same issue before, I had to set the height to 250dp, but I couldn't, so what I did was just wrap it into a FrameLayout and set the custom height to it, you can set the height you want to the wrapper (just not wrap_content) and it will work with no problem:

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="250dp">

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

</FrameLayout>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.