Anchor Android Dialog to a View

Viewed 4529

I wanted to know if it is possible to anchor a Dialog to a View in Android, or in general: just to display the dialog at a certain location on the screen.

P.S. I don't want to use a PopupMenu because it is my understanding that one cannot customize the items displayed in the menu-- I'm ultimately trying to have text and put an image next to it to alert the user that they have a message or something new to see here.

Thanks for your time-

1 Answers

Use Window params.

 WindowManager.LayoutParams params = new WindowManager.LayoutParams();
    params.copyFrom(getDialog().getWindow().getAttributes());
    params.gravity = Gravity.BOTTOM;
    params.y = YOUR_ANCHOR_VIEW.getHeight();
    getDialog().getWindow().setAttributes(params);

I used Gravity.BOTTOM and view height to anchor dialog on top of anchor view.

Gravity.TOP will make y to apply from top and vise versa.

I used this code on onActivityCreated().

Related