That's right, I hit my head on the wall for an hour or two with this, before finally getting the DialogFragment as I wanted.
I will describe Steelight's answer here. This is the easiest and most reliable approach I have found.
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle b) { Window window = getDialog().getWindow(); // set "origin" to top left corner, so to speak window.setGravity(Gravity.TOP|Gravity.LEFT); // after that, setting values for x and y works "naturally" WindowManager.LayoutParams params = window.getAttributes(); params.x = 300; params.y = 100; window.setAttributes(params); Log.d(TAG, String.format("Positioning DialogFragment to: x %d; y %d", params.x, params.y)); }
Note that params.width and params.softInputMode (used in Steelight's answer) are irrelevant for this.
Below is a more complete example. What I really need is to align the โconfirmโ checkbox of the dialog box next to the โsourceโ or โparentโ view, in my case ImageButton.
I decided to use DialogFragment instead of any user fragment, because it gives you "dialog" functions for free (close the dialog when the user clicks outside, etc.).

ConfirmBox example above its "original" ImageButton (trashcan)
public class ConfirmBox extends DialogFragment { private View source; public ConfirmBox() { } public ConfirmBox(View source) { this.source = source; } public static ConfirmBox newInstance(View source) { return new ConfirmBox(source); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setStyle(STYLE_NO_FRAME, R.style.Dialog); } @Override public void onStart() { super.onStart();
It is very simple to make the above more general use by adding constructor or setter options as needed. (My last ConfirmBox has a stylized button (inside some borders, etc.) whose text and View.OnClickListener can be configured in the code.)
Jonik Dec 06 '13 at 8:22 2013-12-06 08:22
source share