Mehvish Ali
Mehvish Ali

Reputation: 752

WebView Not Loading inside Custom Dialog Layout

I am trying to load a pdf document from web to url (which I accessed in browser and its working well), when I load the url in WebView inside Custom AlertDialog my WebView diaplays nothing.

alert_dialog_webview.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="10dp"
    android:paddingStart="10dp"
    android:paddingRight="10dp"
    android:paddingEnd="10dp"
    >
    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/web_view"
        ></WebView>

</RelativeLayout>

code:

   private void showDoc(String fileName) {

        final View view = getActivity().getLayoutInflater().inflate(R.layout.alert_dialog_webview, null);

        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setCancelable(false);

        final WebView webview  = (WebView) view.findViewById(R.id.web_view);
        String url = BASE_URL+fileName;
        //webview.loadUrl(url);
        webview.setWebViewClient(new WebViewClient() {
            @SuppressWarnings("deprecation")
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @TargetApi(Build.VERSION_CODES.N)
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                webview.loadUrl(request.getUrl().toString());
                return true;
            }
        });




        String negativeText = getString(R.string.cancel);
        builder.setNegativeButton(negativeText,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                       dialog.dismiss();
                    }
                });

        final AlertDialog dialog = builder.create();
        dialog.setView(view);
 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialogInterface) {
                dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(ContextCompat.getColor(getContext(),R.color.colorAccentDark));

            }
        });
        // display dialog
        dialog.show();
    }

Upvotes: 0

Views: 1529

Answers (3)

rockar06
rockar06

Reputation: 454

I have the same issue with a WebView inside a AlertDialog, but I notice that WebView is loading the url but its height is very small, so it is difficult to know if is loaded de page or not.

The trick I found was that you need to add a weight property to WebView and use match_parent in the height

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical">

    <WebView
        android:id="@+id/webview_terms"
        android:layout_width="match_parent"
        android:layout_height="match_parent" <!-- use match_parent -->
        android:layout_weight="1" <!-- add this line -->
        tools:ignore="InefficientWeight" />

    <Button
        android:id="@+id/btn_accept_terms"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:background="@drawable/background_button_selector"
        android:text="@string/accept_button"
        android:textColor="@android:color/white" />

</LinearLayout>

Upvotes: 2

Faisal Naseer
Faisal Naseer

Reputation: 4248

Try this :)

private void showDoc(String fileName) {

            final View view = getActivity().getLayoutInflater().inflate(R.layout.alert_dialog_webview, null);

            AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
            builder.setCancelable(false);

            final WebView webview  = (WebView) view.findViewById(R.id.web_view);
            String url = "https://docs.google.com/viewer?url="+BASE_URL+fileName;
            webview.getSettings().setAllowFileAccess(true);
            webview.getSettings().setPluginState(WebSettings.PluginState.ON);
            webview.getSettings().setBuiltInZoomControls(true);
            webview.getSettings().setJavaScriptEnabled(true);
            webview.setWebChromeClient(new WebChromeClient());
            webview.loadUrl(url);

            String negativeText = getString(R.string.cancel);
            builder.setNegativeButton(negativeText,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                           dialog.dismiss();
                        }
                    });

            final AlertDialog dialog = builder.create();
            dialog.setView(view);
            dialog.setOnShowListener(new DialogInterface.OnShowListener() {
                @Override
                public void onShow(DialogInterface dialogInterface) {
                    dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(ContextCompat.getColor(getContext(),R.color.colorAccentDark));

                }
            });
            // display dialog
            dialog.show();
        }

Upvotes: 0

Karan Kalsi
Karan Kalsi

Reputation: 819

Android Webview do not support PDF, although chrome app support it.

But, if you want to open PDF in your app you can use MuPDF Library.

If you still want to open PDF in Webview you can use google drive's url to open the pdf file.

But, its just like a webpage opening the pdf with Google toolbar above on it. Checkout this answer for implementing this:- https://stackoverflow.com/a/42975739/3488710

Upvotes: 0

Related Questions