0

I have a DialogFragment where I create the alertDialog in the onCreate():

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    if (alertDialog == null) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        alertDialog = builder.create();
        alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        alertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        alertDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
        alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
    }
    alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    alertDialog.setView(getDialogLayout(),0,0,0,0);
    return alertDialog;
}

Then I set the width (dialogWidth) of alertDialog in the onStart():

    @Override
public void onStart() {
        super.onStart();
        WindowManager.LayoutParams lp = alertDialog.getWindow().getAttributes();
        lp.width = dialogWidth; 
        lp.x = Constants.iX_PositionDialog;
        lp.y = Constants.iY_PositionDialog;
        alertDialog.getWindow().setAttributes(lp);
    }

In my case i set the width of the dialog to 648 but the canvas/window of my surfaceView is just 590, why? I need the width i set.

Ste
  • 21
  • 1
  • 3

3 Answers3

0

Set the layout after show() method of alertDialog.

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(layout);
builder.setTitle("Title");
alertDialog = builder.create();
alertDialog.show();
alertDialog.getWindow().setLayout(648, 400); //Controlling width and height.

NOTE: Setting the layout after show() is the key point.

For more - how-to-control-the-width-and-height-of-default-alert-dialog-in-android.

Community
  • 1
  • 1
sjain
  • 23,126
  • 28
  • 107
  • 185
  • unfortunately it doesnt work. i can set the width in my way but my problem is: why is the width not accurate. – Ste Jun 23 '14 at 10:10
0

To set the width and height of your alert dialouge for siffrent screen use:

int dialogWidth = getActivity().getResources().getDisplayMetrics().widthPixels-120; // screen width - whatever the width you want to set.
int dialogHeight = getActivity().getResources().getDisplayMetrics().heightPixels -140; //screen height - whatever the width you want to set.
getDialog().setCanceledOnTouchOutside(false);
Window window = getDialog().getWindow();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.gravity = Gravity.CENTER;
lp.height = dialogHeight;
lp.width = dialogWidth;
window.setAttributes(lp);

its a best practice as in andorid there are many devices and of many resolution,so we have to do everythign according to diffrent screen.so that it will be feasible with all types of screens.

Pankaj Arora
  • 10,224
  • 2
  • 37
  • 59
0

I found the solution for my self.

So the alertDialog View is packed in an few FrameLayouts.

the Padding of some of these are not 0.

According of this helping code here :[AlertDialog with custom view: Resize to wrap the view's content

I make it with the following methode which i call in the onStart methode:

protected void forceWrapContent(View v) {
    // Start with the provided view
    View current = v;

    // Travel up the tree until fail, modifying the LayoutParams
    do {
        // Get the parent
        ViewParent parent = current.getParent();    

        // Check if the parent exists
        if (parent != null) {
            // Get the view
            try {
                current = (View) parent;
            } catch (ClassCastException e) {
                // This will happen when at the top view, it cannot be cast to a View
                break;
            }

            // Modify the layout
            current.getLayoutParams().width = dialogWidth;
            current.setPadding(0, 0, 0, 0);

        }
    } while (current.getParent() != null);

    // Request a layout to be re-done
    current.requestLayout();
}

Thanks!

Community
  • 1
  • 1
Ste
  • 21
  • 1
  • 3