Home » Android » How to show a WebView with Theme.Dialog style in Android

How to show a WebView with Theme.Dialog style in Android

Posted by: admin June 15, 2020 Leave a comment

Questions:

I declared a WebView activity in the manifest like this:

<activity android:name=".MyWebView"
    android:label="@string/app_name"
    android:configChanges="orientation|keyboardHidden"
    android:theme="@android:style/Theme.Dialog">
</activity>

The WebView looks like this:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     >
    <WebView android:id="@+id/webview"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
    />
</LinearLayout>

When I start this activity within my main activity, only the title of the dialog, containing the apps name, is visible but not the WebView. If I add a TextView to the LinearLayout, it is shown as well, but still the WebView is missing.
If I don’t apply android:theme="@android:style/Theme.Dialog" in the manifest, the WebView is displayed.

Why is that and how can I show a WebView in a dialog?

How to&Answers:

I found that it works if you change the webview to wrap_content rather than fill_parent. No idea why. It’s probably a bug.

Answer:

The best way I have found is a little trick like this:

Notice the 1000dp for minWidth and minHeight

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:minWidth="1000dp"
          android:minHeight="1000dp" >

    <WebView android:id="@+id/web_view"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent" />

</LinearLayout>

The other way is to wrap content on the WebView but it wont expand until you load a webpage..so it will start small, then expand. This hackish way will make the large dialog at start.

Answer:

I would think what you are doing would work. Maybe try to override the dialog class instead of the activity class.

Answer:

Use the WebView as the root layout – without any surrounding element.
Don’t know why it didn’t work with the surrounding layout.