Wednesday, May 24, 2017

Why does minHeight attribute not work in WebView Android?

Leave a Comment

This question has already been asked here, but it has no solution.

I have a WebView. I want to set minimum height to the WebView using minHeight attribute, but it doesn't work. The same attribute works for Button.

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.anshul.webview.WebActivity">  <WebView     android:id="@+id/webView"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:minHeight="400dp"></WebView>  <Button     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:layout_alignParentBottom="true"     android:minHeight="150dp"     android:text="This is a Button. It's minHeight is set to 150 dp and it works !!"/> 

Clearly from the below image, the WebView is not supporting the minHeight attribute. Does anybody knows a solution for this problem?

enter image description here

6 Answers

Answers 1

First, let's understand how other view's use android:minHeight attribute. Let's take Spinner for example. In AbsSpinner#onMeasure() we code see following chunk of code:

... preferredHeight = Math.max(preferredHeight, getSuggestedMinimumHeight()); preferredWidth = Math.max(preferredWidth, getSuggestedMinimumWidth());  heightSize = resolveSizeAndState(preferredHeight, heightMeasureSpec, 0); widthSize = resolveSizeAndState(preferredWidth, widthMeasureSpec, 30);  setMeasuredDimension(widthSize, heightSize); ... 

So, getSuggestedMinimumHeight() should be regarded when computing preferred height.

Now, let's see how WebView is being measured.

AwLayoutSizer is the last component that is responsible for measuring WebView and we can clearly see, that its onMeasure() does not respect getSuggestedMinimumHeight() value.

I'm not sure whether this is an intended behavior or no. Nevertheless, I cannot find enough seams to somehow affect that measurement process. Here's the chuck of code in WebView class, where the object that eventually would return WebViewChromium (the first step in abovementioned order) is initialized.

      private void ensureProviderCreated() {         checkThread();         if (mProvider == null) {             // As this can get called during the base class constructor chain, pass the minimum             // number of dependencies here; the rest are deferred to init().             mProvider = getFactory().createWebView(this, new PrivateAccess());         }     }  

As you can see, this is not something that can be easily customized/changed.

Answers 2

You can try like this.

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"               android:layout_width="match_parent"               android:layout_height="match_parent"               android:orientation="vertical">     <LinearLayout android:layout_width="match_parent" android:layout_height="400dp">         <WebView             android:id="@+id/webView"             android:layout_width="match_parent"             android:layout_height="wrap_content"></WebView>     </LinearLayout>      <Button         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_alignParentBottom="true"         android:minHeight="150dp"         android:text="This is a Button. It's minHeight is set to 150 dp and it works !!"/> </LinearLayout> 

you can set height of your LinearLayout not webView. Thanks :)

Answers 3

I think webview dimensions work more according to content view port properties...

Check https://developer.android.com/guide/webapps/targeting.html#Viewport

The viewport is the area in which your web page is drawn. Although the viewport's total visible area matches the size of the screen when zoomed all the way out, the viewport has its own pixel dimensions that it makes available to a web page. For example, although a device screen might have physical a width of 480 pixels, the viewport can have a width of 800 pixels. This allows a web page designed at 800 pixels wide to be completely visible on the screen when the viewport scale is 1.0. Most web browsers on Android (including Chrome) set the viewport to a large size by default (known as "wide viewport mode" at about 980px wide). Many browsers also zoom out as far as possible, by default, to show the full viewport width (known as "overview mode").

Answers 4

use constraint layout .that will help to resolve all these types of errors and very easy to use.

if your android studio version is below 2.3.1 then add this dependency.

compile 'com.android.support.constraint:constraint-layout:1.0.0-beta1' 

Answers 5

You can try like this code.

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"               android:layout_width="match_parent"               android:layout_height="match_parent"               android:orientation="vertical">     <LinearLayout android:layout_width="match_parent" android:layout_height="400dp">         <WebView             android:id="@+id/webView"             android:layout_width="match_parent"             android:layout_height="wrap_content"></WebView>     </LinearLayout>      <Button         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_alignParentBottom="true"         android:minHeight="150dp"         android:text="This is a Button. It's minHeight is set to 150 dp and it works !!"/> </LinearLayout> 

Answers 6

Try it :

protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         WebView webView = (WebView)findViewById(R.id.webView);         webView.loadDataWithBaseURL(null, "<html><body bgcolor=\"#E6E6FA\"> hehehe </body></html>", "text/html", "utf-8", null);      }    <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:background="@color/blue"     android:layout_height="match_parent">     <LinearLayout         android:minHeight="300dp"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_alignParentTop="true"         android:layout_alignParentStart="true"         >         <WebView             android:id="@+id/webView"             android:layout_width="match_parent"             android:layout_height="match_parent"/>     </LinearLayout>       <Button         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_alignParentBottom="true"         android:minHeight="150dp"         android:text="This is a Button. It's minHeight is set to 150 dp and it works !!"/>     </RelativeLayout> 

enter image description here

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment