User3
User3

Reputation: 2535

Display a popup at the left edge of a view

I am using a custom popup in a list view, this popup needs to be drawn half way through the height of triggerview and on the left side of the triggerview.

I am able to achieve the first part, second seems elusive, here is my code:

public class QuickAction implements View.OnTouchListener {

    private View triggerView;
    private PopupWindow window;
    protected final WindowManager windowManager;

    public QuickAction(View triggerView) {
        this.triggerView = triggerView;
        window = new PopupWindow(triggerView.getContext());
        window.setTouchable(true);
        window.setTouchInterceptor(this);
        windowManager = (WindowManager) triggerView.getContext().getSystemService(Context.WINDOW_SERVICE);
        ImageView imageView = new ImageView(triggerView.getContext());
        imageView.setImageResource(R.drawable.ic_action_email);


        LayoutInflater layoutInflater = (LayoutInflater) triggerView.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = layoutInflater.inflate(R.layout.quick_action_popup_layout, null, false);


        window.setContentView(layout);
        //  window.setBackgroundDrawable(null);
        window.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
        window.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        window.setTouchable(true);
        window.setFocusable(false);
        window.setOutsideTouchable(true);
    }

    public boolean onTouch(View v, MotionEvent event) {
        if (MotionEvent.ACTION_OUTSIDE == event.getAction()) {
            this.window.dismiss();
            return true;
        }
        return false;
    }

    public void show() {
        int[] location = new int[2];
        triggerView.getLocationOnScreen(location);
        window.showAtLocation(triggerView, Gravity.NO_GRAVITY, location[0] + (triggerView.getLeft()), location[1] + (triggerView.getHeight() / 2));
    }

}

What I want is to align the right edge of my popup window to the left egde of my triggerview

Triggerview is my button where I want to generate the popup, the relevant parts of code being:

 public void show() {
        int[] location = new int[2];
        triggerView.getLocationOnScreen(location);
        window.showAtLocation(triggerView, Gravity.NO_GRAVITY, location[0] + (triggerView.getLeft()), location[1] + (triggerView.getHeight() / 2));
    }

However this does nto give me the desired result. Any ideas?

Upvotes: 1

Views: 1706

Answers (1)

Markus Rubey
Markus Rubey

Reputation: 5238

Specifying a gravity of NO_GRAVITY is similar to specifying Gravity.LEFT | Gravity.TOP.

If you want to place the popup window's right side to your specified location you need Gravity.TOP | Gravity.RIGHT instead of Gravity.NO_GRAVITY.

Upvotes: 1

Related Questions