android

android: set margin on PopupWindow by code


I want to set left and right margins on popupwindow. I try setting layout params on layout , and then set margin , but not work.

Can anyone give me code for setting margin on popupwindow

Here is code

LayoutInflater inflater = (LayoutInflater) QuestionsActiviy.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.popup, (ViewGroup) findViewById(R.id.popup_element));


        ratePw = new PopupWindow(layout);
        ratePw.setWidth(WindowManager.LayoutParams.FILL_PARENT);
        ratePw.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        ratePw.setFocusable(true);

        ratePw.showAtLocation(layout, Gravity.CENTER, 0, 0);

Thanks.


Solution

  • as your layout is top on window, and you are doing this dynamically, by using width and height of screen, so to set margin 10 on sll side you can use:

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;
    
    LayoutInflater inflater = (LayoutInflater) QuestionsActiviy.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.popup, (ViewGroup) findViewById(R.id.popup_element));
    
    
    ratePw = new PopupWindow(layout);
    ratePw.setWidth(width-20);
    ratePw.setHeight(height-20);
    ratePw.setFocusable(true);