Code Geek Code Geek - 2 months ago 72
Android Question

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

Answer

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" >
Comments