user519846
user519846

Reputation: 1009

How to load web view in dialoge in android

I am trying to load web view in to dialog. I am usuing the following code.

    final TextView seeMonthlyBill = (TextView) parentLayout
            .findViewById(R.id.my_ac_my_service_timewarnercable_link);
    seeMonthlyBill.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Dialog dialog = new Dialog(getActivity());
            WebView wb = new WebView(getActivity());
            wb.getSettings().setJavaScriptEnabled(true);
            wb.loadUrl("http://www.google.com");
            wb.setWebViewClient(new HelloWebViewClient());
            dialog.setCancelable(true);
            dialog.setContentView(wb);
            dialog.setTitle("WebView");
            dialog.show();
        }
    });

I want to open web view on the click on a text view. When i am clicking on the text view dialog is opening without the webview. Can any please help me to solve this issue.

Thanks in advance.

Upvotes: 1

Views: 10066

Answers (4)

Lokesh Gupta
Lokesh Gupta

Reputation: 301

Set webview's height and width and it will work.

Upvotes: 1

Natie
Natie

Reputation: 2284

Use a XML layout containing a Webview. Then call dialog.setContentView(R.layout.web_dialog)

Set up the webview afterwards like so: WebView wv = (WebView) findViewById(R.id.webview); "R.id.webview" being the ID in the XML Layout file.

Dialog Layout example:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
>
    <WebView
        android:id="@+id/webview"
        android:scrollbars="vertical"
        android:scrollbarAlwaysDrawVerticalTrack="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />
</LinearLayout>

Your code modified:

final TextView seeMonthlyBill = (TextView) parentLayout
            .findViewById(R.id.my_ac_my_service_timewarnercable_link);
    seeMonthlyBill.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Dialog dialog = new Dialog(getActivity());
            dialog.setContentView(R.layout.web_dialog)
            WebView wb = (WebView) dialog.findViewById(R.id.webview);
            wb.getSettings().setJavaScriptEnabled(true);
            wb.loadUrl("http://www.google.com");
            wb.setWebViewClient(new HelloWebViewClient());
            dialog.setCancelable(true);
            dialog.setTitle("WebView");
            dialog.show();
        }
    });

Upvotes: 5

Bugs bunny
Bugs bunny

Reputation: 163

@Override

        protected Dialog onCreateDialog(int id) {

            // TODO Auto-generated method stub
                Dialog dialog = new Dialog(yourActivity.this);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                setDialogView(dialog,id);
                return dialog;
            return super.onCreateDialog(id);

        }


    private void setDialogView(final Dialog dialog,final int id) {

        // TODO Auto-generated method stub

        dialog.setContentView(R.layout.layoutContainingWebview);
        final WebView mWebView = (WebView)dialog.findViewById(R.id.WebviewId);
        mWebView.getSettings().setBuiltInZoomControls(true);
        mWebView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
        mWebView.setBackgroundColor(Color.TRANSPARENT);
        mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_INSET);
        mWebView.loadUrl("url");
        mWebView.setWebViewClient(new MyWebViewClient());
        dialog.setTitle("Feedback");  // for Feedback           
    }


    private class MyWebViewClient extends WebViewClient { 

        @Override 
        //show the web page in webview but not in web browser
        public boolean shouldOverrideUrlLoading(WebView view, String url) { 
            view.loadUrl (url); 
            return true;
        } 

TO show call showDialog(1);

Upvotes: 1

coder_For_Life22
coder_For_Life22

Reputation: 26971

What i would recommend is creating an activity with a webview layout and anything else you want to add to it.

And when you register it in the android manifest use this tage.

<activity android:theme="@android:style/Theme.Dialog">

Upvotes: -1

Related Questions