harish
harish

Reputation: 2483

Change the width of the Dialog window of Android app dynamically

I am creating an Android application. In that application, I am using a Dialog window to display HTML contents which come from the sever side. I created the Dialog window using below method.

private void displayDialogWindow(String html) {

        // here I have created new dialog window
        dialog = new Dialog(cordova.getActivity(),
                android.R.style.Theme_NoTitleBar);
        dialog.getWindow().getAttributes().windowAnimations =
android.R.style.Animation_Dialog;
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(true);


        // creates a new webview to display HTML and attached to
dialog to display
        webview = new WebView(cordova.getContext());
        webview.setLayoutParams(new LinearLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        webview.setWebChromeClient(new WebChromeClient());
        webview.requestFocusFromTouch();

        final String mimeType2 = "text/html";
        final String encoding2 = "UTF-8";
        webview.loadDataWithBaseURL("", html, mimeType2, encoding2, "");
        dialog.setContentView(webview);

        // get the screen size of the device
        WindowManager wm = (WindowManager) ctx
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int width = size.x;
        int height = size.y;

        // set appropriate sizes to the dialog window whether there is
side menu or not
        lp.copyFrom(dialog2.getWindow().getAttributes());
        lp.width = width ;
        lp.height = height - 100;
        dialog.getWindow().setAttributes(lp2);

        dialog.show();
    }

Using above method I could successfully create a Dialog window.

And I am creating the Dialog window using a Thread,

Runnable runnable = new Runnable() {
public void run() {

                displayDialogWindow(html);

            }
};

But after display the Dialog window once, I want to change the width of the Dialog window dynamically. (eg. when I click on another button of my app, the width of the window should be decreased and come to the original width when another button is pressed).

Can any one help me to change the width of the Dialog window dynamically?

Upvotes: 2

Views: 4141

Answers (1)

JJ86
JJ86

Reputation: 5113

I think that you have to "recreat" your dialog everytime you want to change it. After yourDialog.show() method, you can insert this line:

yourDialog.getWindow().setLayout((6 * width)/7, LayoutParams.WRAP_CONTENT);

This allow you to resize your Dialog window and it's content. Of course width and height are the size of your screen.

Upvotes: 3

Related Questions