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
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:
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.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();
}
}
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" >
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With