Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Positioning a dialog on screen

Tags:

android

dialog

I have an activity with MapView. I have overlays on that map and I would like to position a hovering dialog over an item if it is tapped.

I already have my own custom dialog I've made up with my own layout and custom background. My only problem is how to tell Android where (in x, y terms) to position the dialog.

It's even more complicated since to know what's the dialog x, y. I first need to know the dialog's width and height after measurement (since I need that to calculate the position relative to my overlay).

like image 738
codeScriber Avatar asked Dec 23 '10 13:12

codeScriber


1 Answers

A quick Google search came up with the following:

You can call getWindow().getAttributes() to retrieve the WindowManager.LayoutParams for the window. This has the following fields involving window placement:

http://code.google.com/android/reference/android/view/ViewGroup.LayoutParams.html#width http://code.google.com/android/reference/android/view/ViewGroup.LayoutParams.html#height http://code.google.com/android/reference/android/view/WindowManager.LayoutParams.html#gravity http://code.google.com/android/reference/android/view/WindowManager.LayoutParams.html#x http://code.google.com/android/reference/android/view/WindowManager.LayoutParams.html#y

After making your desired changes, use getWindow().setAttributes() to install the new values.

Note that, though you can force a specific size through the width and height fields, in general the correct way to do this is let the window do its normal layout and determine the window size automatically that way. If you have a single view in the dialog that wants a fixed size such as 300x200, implement View.onMeasure() to call setMeasuredDimension(300, 200). Then when the view hierarchy layout happens, the dialog window will be given a size that ensures your view is that dimension (probably making the actual window larger to take into account the dialog frame and decoration).

Hope that helps.

like image 64
user432209 Avatar answered Oct 21 '22 06:10

user432209