Reputation: 4794
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.
Upvotes: 11
Views: 25335
Reputation: 172
I know I'm late, but the below code worked for me
popupWindow.apply {
activity?.let {
val xOffSet = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
-20dp, it.resources.displayMetrics
).roundToInt()
val yOffSet = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
-10dp,
it.resources.displayMetrics
).roundToInt()
contentView = allRewardsOptionsMenuBinding.root
showAsDropDown(anchorView, xOffSet, yOffSet)
}
}
Upvotes: 0
Reputation: 141
I'm also facing this issue. and after spending about 2 hours I have done it on this way.
private void setPopUpWindow(View v) {
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.dialog_menu_overflow, null);
int width = ConstraintLayout.LayoutParams.WRAP_CONTENT;
int height = ConstraintLayout.LayoutParams.WRAP_CONTENT;
boolean focusable = true;
popupWindow = new PopupWindow(view, width, height, focusable);
popupWindow.showAsDropDown(v, 0, -customToolbar.getHeight());
}
And in layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:padding="@dimen/_6sdp"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription"
android:background="@drawable/popup_menu_rounded_bg">
Upvotes: 0
Reputation: 11
My only solution was to add in custom_popup.xml layout 2 transparent relativeLayouts with width and height - 20dp(or else). Just set them to right and top of main layout.
Also you can use popup.showAsDropDown(anchorVIew, 480, -100) and allocate position manually instead of showAtLocation()
Upvotes: 0
Reputation: 29199
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);
Upvotes: 22
Reputation: 1376
LayoutParams parm1=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
parm1.setMargins(20, 20, 0, 0);
layout.setLayoutParams(parm1);
//ratePw.addView(layout,parm1); // removed it
ratePw.setContentView(layout);
Actually,the format of layoutParams.setMargins(left,top,right,bottom);
Upvotes: -1