Error: Android-XML: placing <WebView> in the parent element that uses the wrap_content size can lead to subtle errors; use match_parent

I am new to Android and create a linear layout and get an error in the XML layout file, e.g.

Error

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

The 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 complete XML file code

 <?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 someone tell me what is wrong in my code and how can I get rid of the error?

I think this may be a very simple question, but I tried and could not understand.

More details:

API level: API 19: Android 4.2.2

+6
source share
4 answers

The problem basically is that in the parent LinearLayout you provide layout_width and layout_height as wrap_content . It must 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" > 
+9
source

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

I have screens that consist of layered web views because their content comes from different sources. My screen layouts “wrap content” for height so that the user always sees what information comes from different sources without huge areas of space. Its an unusual design, but one that worked well for 4 years on many similar applications. Yes, I know that I have to be careful about height, etc., but I am happy to assume this responsibility.

Eclipse essentially creates a “Nanny State” - this is not how we usually do it, so it’s wrong. Forgive me, but I have thought differently.

The way I got around this is to close the XML file and completely clear the project. Thus, Eclipse forgets that it’s ever “nanny nagged” about this problem and everything is fine again.

+7
source

Unlike most answers, this is not an error in Eclipse or. Android Studio, but a fair warning. It is created by checking LINT on your layout.

You can remove the warning and work on "fake errors", for example:

  • Add tools:ignore="WebViewLayout" to your WebView (thanks to @StefanDeitmar) and make the tool namespace famous by adding xmlns:tools="http://schemas.android.com/tools" to your external layout element.
  • In your code, add a WebViewClient to your WebView and implement the OnPageFinished() callback to call requestLayout() on the packaging layout element.

.

 mWebView.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView webView, String url) { super.onPageFinished(webView, url); mSurroundingLayout.requestLayout(); } ); 
+5
source

I had the same problem before, I needed to set the height to 250dp, but I couldn’t, so I did it, just put it in FrameLayout and set a custom height for it, you can set the height you want to wrap (just not wrap_content) and it will work without problems:

 <FrameLayout android:layout_width="250dp" android:layout_height="match_parent"> <WebView android:id="@+id/web_view_tutorial" android:layout_width="match_parent" android:layout_height="match_parent"></WebView> </FrameLayout> 
0
source

Source: https://habr.com/ru/post/977636/


All Articles