How to load web view in dialog box in android

I am trying to load a web view into a dialog. I am using 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 a web view when I click on a text view. When I click on the text view dialog box, it opens without a web view. Can anyone help me solve this problem.

Thanks in advance.

+1
android android-webview android-dialog
source share
4 answers

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

After that, configure the web view as follows: WebView wv = (WebView) findViewById (R.id.webview); "R.id.webview" is the identifier in the XML layout file.

Dialog example layout:

 <?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 has been changed:

 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(); } }); 
+5
source share
 @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 a call to showDialog (1);

+1
source share

Set the height and width of the webview and it will work.

+1
source share

What I would recommend is to create an action with a webview layout and all that 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"> 
-one
source share

All Articles