1

I'm trying to put webview in alertdialog, I'm using code I found somewhere on the stack but for some reason, it doesn't work.

Here's an example of my code:

holder.textView4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder alert = new AlertDialog.Builder(context);
                alert.setTitle("Google:");

                View view = LayoutInflater.from(context).inflate(R.layout.webLayout, null);

                WebView wv = view.findViewById(R.id.webview);

                wv.loadUrl(link);
                wv.setWebViewClient(new WebViewClient() {
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView v, String url) {
                        v.loadUrl(url);
                        return true;
                    }
                });

                alert.setView(view);
                alert.setNegativeButton("Close", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.dismiss();
                    }
                });
                alert.create().show();
            }
        });

webLayout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </WebView>

</LinearLayout>

After click on button only empty dialog shows:

enter image description here

Niklaus
  • 159
  • 1
  • 2
  • 11

3 Answers3

2

Have you added the INTERNET Permission to the Manifest? <uses-permission android:name="android.permission.INTERNET"/>

You got to create an xml layout fisrt, and put the webview in the layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--Configure the webview as you want-->
    <WebView
        android:id="+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </WebView>

</LinearLayout>

create a view object instead a webview

View view = LayoutInflater.from(context).inflate(R.layout.layout_with_webview , null)

Now to crete a webview object use WebView webView = view.findViewById(R.id.webview) Pass that view to alert.setView(view)

And the most important alertDialogBuilder.show()

daniel.jbatiz
  • 437
  • 4
  • 18
0

Just replace

alert.create().show();

into

alert.show();

Update:

Please use https:// in your url rather http:// or blank. Also you need to add clear text support if you really want to use non secure url. Please see how to configure here (https://stackoverflow.com/a/50834600/1084174)

Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256
0

Try this in AndroidManifest.xml

  <application
        android:name="com.****.***"
        ****************
        ************
        *******
        .
        .
        .
        android:usesCleartextTraffic="true"
        >
Ak23
  • 342
  • 1
  • 5
  • 17