How to open webview in popupwindow in android?

I want to add a web view to PopupWindow in my Android application. This only works for the warning dialog. And this is my code:

AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this); alert.setTitle("Google"); WebView wv = new WebView(MainActivity.this); wv.loadUrl("http:\\www.google.com"); wv.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }); alert.setView(wv); alert.setNegativeButton("Close", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } }); alert.show(); } 

But I want to do this with a popup.

0
source share
4 answers
 textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this); LayoutInflater inflater = MainActivity.this.getLayoutInflater(); final View dialogView = inflater.inflate(R.layout.main, null); WebView wv = (WebView) dialogView.findViewById(R.id.web); wv.loadUrl("https://www.google.com"); wv.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }); dialogBuilder.setView(dialogView); Dialog markerPopUpDialog = dialogBuilder.create(); markerPopUpDialog.show(); } }); 

and this is layout.main

 <?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/web" android:layout_width="match_parent" android:layout_height="match_parent"> </WebView> 

0
source

you can do this using activity as well. first you need to do a web view and set the width and height of the layout in the java file using the code below.

  DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); int width = dm.widthPixels; int height = dm.heightPixels; getWindow().setLayout((int) (width*0.9),(int)(height*0.8)); 

and in the Android manifest file, select the theme "@ style / AppTheme.CustomThemeForWebViewPOPUp" for this action and put this code in the style file ....

 <style name="AppTheme.CustomThemeForWebViewPOPUp"> <item name="android:windowIsTranslucent">true</item> <item name="android:windowCloseOnTouchOutside">true</item> <item name="android:windowBackground">@drawable/popup_activity_design</item> <item name="android:backgroundDimEnabled">true</item> </style> 
0
source

According to your requirement, you should customize the warning dialog with a custom view.

Make one xml file:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerInParent="true"> </WebView> <ProgressBar android:id="@+id/prg" android:layout_width="25dp" android:layout_height="25dp" android:layout_centerInParent="true" /> </RelativeLayout> <Button android:id="@+id/button3" style="@android:style/Widget.Holo.Button.Borderless" android:layout_width="wrap_content" android:textColor="#000000" android:layout_height="wrap_content" android:layout_gravity="center|right" android:text="Close" /> </LinearLayout> 

Now we implement the code in the java file:

  final AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this); alert.setTitle("Google"); View view = getLayoutInflater().inflate(R.layout.file_name, null); alert.setView(view); WebView wv = (WebView) view.findViewById(R.id.webview); pb = (ProgressBar) view.findViewById(R.id.prg); Button btnClose = (Button) view.findViewById(R.id.button3); pb.setVisibility(View.VISIBLE); btnClose.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { alertDialog.dismiss(); } }); wv.setWebViewClient(new MyWebViewClient()); wv.loadUrl("http://www.google.com"); alertDialog = alert.create(); alertDialog.show(); WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); lp.copyFrom(alertDialog.getWindow().getAttributes()); lp.width = WindowManager.LayoutParams.MATCH_PARENT; lp.height = WindowManager.LayoutParams.MATCH_PARENT; alertDialog.getWindow().setAttributes(lp); 

Also create a custom WebViewClient

 class MyWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); if (pb.getVisibility() == View.GONE) { pb.setVisibility(View.VISIBLE); } return true; } @Override public void onPageFinished(WebView view, String url) { System.out.println("on finish"); if (pb.getVisibility() == View.VISIBLE) { pb.setVisibility(View.GONE); } } } 

Note. Declare an alertDialog object as global.

0
source

Finally, I did it. Here is my code if someone needs it.

  AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this); alert.setTitle("Terms and conditions"); View view= LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_termsdialog,null); WebView wv = (WebView) view.findViewById(R.id.web); progressbarterms= (ProgressBar) view.findViewById(R.id.progressbarterms); wv.loadUrl("my url"); WebSettings webSettings = wv.getSettings(); webSettings.setJavaScriptEnabled(true); wv.setWebViewClient(new TermsClient()); alert.setView(view); alert.setNegativeButton("Close", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } }); alert.show(); } class TermsClient extends WebViewClient { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { // TODO Auto-generated method stub super.onPageStarted(view, url, favicon); } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // TODO Auto-generated method stub progressbarterms.setVisibility(View.VISIBLE); view.loadUrl(url); return true; } @Override public void onPageFinished(WebView view, String url) { // TODO Auto-generated method stub super.onPageFinished(view, url); progressbarterms.setVisibility(View.GONE); } } 

And the layout file contains webview and progressbar.

0
source

Source: https://habr.com/ru/post/1275850/


All Articles