How to create a dialog at the top of a button

I want to create a dialog that appears when the user clicks a button. (In fact, this is the kind of volume control that appears when the user presses the button with the headphones)

I have a layout file for a dialog (with content dependent height) and in the onCreate method I am trying to set the gravity top | right and custom margin (same as my button).

this.getWindow().setGravity(Gravity.TOP | Gravity.RIGHT);
WindowManager.LayoutParams lp = this.getWindow().getAttributes();
lp.x = x;
lp.y = y;

      

But what I get is just a centered dialog on the right (not right at the top!) Without any margin.

In other words, I would like to draw my dialog with a custom layout at a specified point on the screen.

+3


source to share


2 answers


The easiest way to achieve this is using Relative Layout

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(WIDTH, HEIGHT);
params.topMargin = X;
params.leftMargin = Y;
yourRelativeLayout.addView(myDialog, params);

      



X should be something like clickedX - WIDTH / 2 ...

Hope this was helpful!

+1


source


I think you are looking for a popup. Look at here:

http://developer.android.com/reference/android/widget/PopupWindow.html



Popup positioning in Android

If you have problems with the placement of the popup, try using the showAsDropDown (View, int offsetx, offsety) method for the PopupWindow, it will bind the window to this view at a specific offset.

+1


source







All Articles