user2609847
user2609847

Reputation: 185

dialog not setting layout params

using a dialog as a window I tried to set its layout as

WindowManager.LayoutParams lp = new WindowManager.LayoutParams();

    lp.x = 120;
    lp.y = 120;
    lp.width = 800;
    lp.height = 450;
    Dialog dialog = new Dialog(cxt);
    dialog.getWindow().setAttributes(lp);
    dialog.setContentView(vFirst);
    dialog.show();

where

View  vFirst = inflater.inflate(R.layout.popup, container, false);

which is inside a button click,but it is not setting it.

Upvotes: 2

Views: 7748

Answers (4)

DeepakPanwar
DeepakPanwar

Reputation: 1389

//dialog width and height are set according to screen, you can take you want

Dialog myDialog = new Dialog(context, R.style.CustomTheme);
            myDialog.setContentView(R.layout.share_post_dialog);

            int[] widthHeight = getDeviceWidthAndHeight(context);
            int dialogHeight = widthHeight[1] - widthHeight[1] / 4;
            int dialogWidth = widthHeight[0] - widthHeight[0] / 5;


        WindowManager.LayoutParams params = myDialog.getWindow().getAttributes();
            params.width= dialogWidth;
            params.height = dialogHeight;
            myDialog.getWindow().setAttributes((WindowManager.LayoutParams) params);

myDialog.show();

//This is used to get height width of screen

@SuppressWarnings("deprecation")
    public static int[] getDeviceWidthAndHeight(Context context) {

        int widthHeght[] = new int[2];
        int measuredWidth = 0;
        int measuredHeight = 0;

        WindowManager w = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {

            Point size = new Point();
            w.getDefaultDisplay().getSize(size);
            measuredWidth = size.x;
            measuredHeight = size.y;
            widthHeght[0] = measuredWidth;
            widthHeght[1] = measuredHeight;

        } else {
            Display d = w.getDefaultDisplay();
            measuredWidth = d.getWidth();
            measuredHeight = d.getHeight();
            widthHeght[0] = measuredWidth;
            widthHeght[1] = measuredHeight;

        }
        return widthHeght;
    }

Upvotes: 0

iramm
iramm

Reputation: 148

According to documentation: http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html,

  1. X position for this window. With the default gravity it is ignored. When using LEFT or START or RIGHT or END it provides an offset from the given edge.

  2. Y position for this window. With the default gravity it is ignored. When using TOP or BOTTOM it provides an offset from the given edge.

So to make x and y work you should set Gravity attribute before. For example:

    lp.gravity = Gravity.TOP;
    lp.gravity = Gravity.LEFT;
    lp.y = 120;
    lp.x = 120;

Upvotes: 0

rajahsekar
rajahsekar

Reputation: 914

public class Myclass extends Dialog
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LayoutParams params = getWindow().getAttributes();
        params.height = LayoutParams.FILL_PARENT;
        getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);

    }
}

layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:id="@+id/dialogWidth"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

   // contents here

</LinearLayout>

Upvotes: 0

Kailash Dabhi
Kailash Dabhi

Reputation: 3513

I think you should use this

  dialog.getWindow().setLayout(800, 450); 

This will set the dialog window size.

Upvotes: 2

Related Questions