0

How would I open my dialog so its in a set position below a Button widget?

This is what I have so far:

    public void showPopup(View v){

    mDialog.setContentView(R.layout.dialog_custom_workout_info);
    Window window = mDialog.getWindow();
    window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    window.setGravity(Gravity.CENTER_HORIZONTAL);
    window.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    mDialog.getWindow().setDimAmount(0);
    mDialog.show();

}

The Dialog opens, but I can only use the gravity to center it, but I would like it opening at a set position relative to another widget ( in this case the widget is labelled "imageButton".

Thanks in advance!

Ajay Taak
  • 15
  • 4

1 Answers1

0

There is an x,y available for position of the dialog, could you use some offset from the x,y of the button you want to position it under? I am sure there are other ways but if it works there is not much modification to your existing code

position of a control

            Dialog d = DialogAbout.create(this);
            Button b = findViewById(R.id.pick_button);
            // Absolute coordinates
            int[] location = new int[2];
            b.getLocationOnScreen(location);
            int x = location[0];
            int y = location[1];

           // Coordinates relative to parent
            int bx = b.getLeft();
            int by = b.getTop();

            Window window = d.getWindow();

            WindowManager.LayoutParams wlp = window.getAttributes();
            // set the new location [you will need to play with this]
            wlp.x = x;
            wlp.y = (y - b.getHeight());

            // add to your window
            window.setAttributes(wlp);