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.