Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android: set margin on PopupWindow by code

Tags:

android

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.

like image 284
Jovan Avatar asked Mar 02 '12 10:03

Jovan


1 Answers

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);
like image 141
jeet Avatar answered Oct 15 '22 15:10

jeet