I want to add web view in PopupWindow in my android application.This is only working for alert 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 popup window.
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>
you can do this by using activity also. first you need to make an activity with web view and set layout width and height in java file with below code....
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 Android Manifest file select theme "@style/AppTheme.CustomThemeForWebViewPOPUp" for this activity and in style file put this code....
<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>
As per your requirement you should customize your alert dialog with 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 implement code in 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 make 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 alertDialog
object as a global.
Finally, I have done it. Here is my code if somebody need to do this.
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 layout file contains webview and progressbar.