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?
What is use in Android? By default, WebView does not implement JavaScript alert dialogs, ie. alert () will do nothing. In order to make you need to firstly enable JavaScript (obviously..), and then set a WebChromeClient to handle requests for alert dialogs from the page:
Apply a style as a theme. You can create a theme the same way you create styles. The difference is how you apply it: instead of applying a style with the style attribute on a view, you apply a theme with the android:theme attribute on either the <application> tag or an <activity> tag in the AndroidManifest.xml file.
Android Themes. Hope you understood the concept of Style, so now let's try to understand what is a Theme. A theme is nothing but an Android style applied to an entire Activity or application, rather than an individual View.
Start by opening the layout resource file where you want to display your WebView, and then add the following: You’ll also find a WebView component in Android Studio’s palette. Then, in the XML file’s corresponding Activity, you’ll need to obtain a reference to your WebView and specify the URL you want it to display:
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.
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