3

I want to create a dialog which appears when user presses a button. (In fact it's kind of a volume control appearing when user clicks the button with headphones)

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

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 top-right!) without any margin.

In other words I'd like to draw my dialog with custom layout at specified point on the screen.

leshka
  • 1,764
  • 6
  • 32
  • 42

2 Answers2

1

The easiest way to achieve this is by using a Relative Layout

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

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

Hope this was helpfull!

Ferdau
  • 1,524
  • 1
  • 12
  • 21
1

I think what you are looking for is a popup window. Have a look here:

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

Positioning a Popup Window in Android

If you are having problems placing the popup window, try using the method showAsDropDown(View, int offsetx, offsety) for PopupWindow, it will anchor the window to the given view by the specific offset.

Community
  • 1
  • 1
Line
  • 293
  • 1
  • 4
  • 13