Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does minHeight attribute not work in WebView Android?

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

like image 946
thedarkpassenger Avatar asked May 11 '17 13:05

thedarkpassenger


People also ask

How do I enable JavaScript on Android WebView?

Enable JavaScript JavaScript is disabled in a WebView by default. You can enable it through the WebSettings attached to your WebView . You can retrieve WebSettings with getSettings() , then enable JavaScript with setJavaScriptEnabled() . WebView myWebView = (WebView) findViewById(R.

How do you override a WebView?

If you want to override certain methods, you have to create a custom WebView class which extends WebView . Also, when you are inflating the WebView , make sure you are casting it to the correct type which is CustomWebView . CustomWebView webView = (CustomWebView) findViewById(R. id.

Is Android WebView deprecated?

The Android system webview custom cache file has been deprecated and removed in Android 13. New apps and any app updates will now use the operating system default cache location.

What is WebView link?

WebView is a view that displays web pages inside the application. It is used to turn the application into a web application.


1 Answers

First, let's understand how other view's use android:minHeight attribute. Let's take Spinner for example. In AbsSpinner#onMeasure() code we 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.

  • WebView#onMeasure() delegates the job to WebViewChromium#onMeasure()
  • WebViewChromium#onMeasure() delegates the job to AwContents#onMeasure()
  • AwContents#onMeasure() delegates the job to AwLayoutSizer#onMeasure

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.

like image 58
azizbekian Avatar answered Oct 23 '22 10:10

azizbekian