0

In the method onOptionsItemSelectedMenu I have a AlertDialog.Builder I am opening a WebView inside this AlertDialog.

However, I want to change the size of the AlertDialog.Builder, I want to change width and height according to my decision.I read a lot of questions here, but strangely nothing worked. Especially this article: How to make an alert dialog fill 90% of screen size? How can I change the size of it ? I want to change it programmatically. I don't inflate any layout or anything else.

Here is my little code:

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_inner_basics) {


            AlertDialog.Builder alert = new AlertDialog.Builder(this);

            android.webkit.WebView wv = new WebView(this);
            wv.loadUrl("file:///android_asset/test.html");

            AlertDialog alertDialog = alert.create();
            alertDialog.setView(wv);
            alertDialog.show();


        }


        return super.onOptionsItemSelected(item);
    }
Blnpwr
  • 1,793
  • 4
  • 22
  • 43

3 Answers3

1

You have to resize a dialog after calling Dialog.show(). I usually create a new class extends from AppCompatDialog for dialog. In the bellow code, I override the show() method and put the resizeDialog(); at the end of show() method.

public class LocationDialog extends AppCompatDialog {

    public LocationDialog(Activity activity) {
        super(activity);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.dialog_layout);

        initView();
    }

    private void initView(){
        // To get WebView from dialog_layout.xml
        // And showing website in this view
        WebView myWebView = (WebView) findViewById(R.id.webview);
        myWebView.loadUrl("http://www.example.com");
    }

    @Override
    public void show() {
        super.show();

        resizeDialog();
    }

    /**
     * To resize the size of this dialog
     */
    private void resizeDialog() {
        try {
            Window window = getWindow();

            if (activity == null || window == null) return;

            DisplayMetrics displayMetrics = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

            int height = displayMetrics.heightPixels;
            int width = displayMetrics.widthPixels;

            window.setLayout((int) (width * 0.95), (int) (height * 0.85));
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));// make tranparent around the popup
        } catch (Exception ignore) {
            ignore.printStackTrace();
        }
    }
}

dialog_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>

In your activity, you should do:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_inner_basics) {
        LocationDialog dialog = new LocationDialog(Activity);
        dialog.show();
    }


    return super.onOptionsItemSelected(item);
}
John Le
  • 1,116
  • 9
  • 12
  • Thank you for your answer, I will just try it but one question. What is `R.layout.dialog_layout` ? I don't have any layout to inflate or to set in setContentView. – Blnpwr Oct 03 '17 at 15:49
  • Thanks for your edit. I will take a look and wll tell you if it worked. – Blnpwr Oct 03 '17 at 15:56
  • 1
    You can create a new one and put the `WebView` in this. I have updated my answer. – John Le Oct 03 '17 at 15:57
1

Here is how! Since AlertBuilder will always take the whole View given hence lets get the device size first so that to make it possible not to vary with device sizes. This method will return device size in terms of an array of int. define this in your activity .

private int[] getScreenSIze(){
    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int h = displaymetrics.heightPixels;
    int w = displaymetrics.widthPixels;

    int[] size={w,h};
    return size;
}

If you are int a fragment consider chaning one line in the method into

getActivity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);

if you are in activity ignore the line.

And when the user clicks option item selected We are going to set the height and width of our webview as 70% device height and 80% device width and since the webview is in Alert Builder. Alert Builder will expand the cover the webView as follows:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_inner_basics) {


        AlertDialog.Builder builder = new AlertDialog.Builder(this); 

        int[] screenSize= getScreenSIze(); //This is the method above remember
        int width=screenSize[0];
        int height=screenSize[1];

        int webViewWidth=(int)(0.8*width); //Casting to make them int
        int webViewHeight=(int)(0.7*height);

        android.webkit.WebView wv = new WebView(this);

        wv.setMinimumHeight(webViewHeight);
        wv.setMinimumWidth(webViewWidth);

        wv.loadUrl("file:///android_asset/test.html");

        builder.setView(wv);
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
       builder.show();
    }


    return super.onOptionsItemSelected(item);
}

I hope it works you can control the height and width as you wish to Appear by chnaging 0.7 and 0.8. Happy Coding!

Xenolion
  • 12,035
  • 7
  • 33
  • 48
  • Its better you change the size of webView during its creation than getting the whole device window during creation. – Xenolion Oct 03 '17 at 15:41
  • Unfortunately, it does not work. Even if I change width to 0.1 , nothing changes. It remains at the same size. – Blnpwr Oct 03 '17 at 15:48
  • Ooh I forgot you were using a framework alert dialog while I continue with AlertBuilder. I have edited the answer. Now it will work! I have used the alert builder fully. Its faster than a pre-made alert dialog! – Xenolion Oct 03 '17 at 16:30
0

One possible way would be to show your content in a new activity with the style Theme.AppCompat.Light.Dialog

Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
  • Is there a way doing it without creating any new activity rather than layout? Isn't it possible to set the size of AlertDialog by using methods which come from AlertDialog.Builder – Blnpwr Oct 03 '17 at 14:51