How to show the PopupWindow alway above the button(view)?

zh_gino picture zh_gino · Apr 8, 2012 · Viewed 10.6k times · Source

The Design requirements is,have a listview,in the items of listview,have a button,press the button, and then,to show a popupwindow above the button alway,not bottom.

In Android,use "showAsDropDown",the Popupwindow default display on bottom of the button(View or anchor). But if the bottom not enougn to show,the Popupwindow will show on top of the button(View or anchor).

onTop = (displayFrame.bottom - mScreenLocation[1] - anchor.getHeight() - yoff) <(mScreenLocation[1] - yoff - displayFrame.top);

so,i according to this point,to move item of the button by "setSelectionFromTop",let the judgment is no enougn to show on bottom of the button to achieve the effectiveness。

In android 4.0.3,is Ok,the item move,and the popupwindow show the new position and aboved, But, in android 2.2,the popupwindow is still show the press Where,not the position after moving。

boolean onTop = (displayFrame.bottom - mScreenLocation[1] - v.getHeight() - 0) < (mScreenLocation[1] - 0 - displayFrame.top);
if(!onTop){
mListMain.setSelectionFromTop(mListMain.getPositionForView(v),(displayFrame.bottom - v.getHeight() + displayFrame.top) / 2 );
}

can help me,how to solve it?!.. T_T

Answer

Fedir Tsapana picture Fedir Tsapana · Oct 10, 2012

may be not same situation, but my solution is:

public class BaloonView extends PopupWindow {   
    public BaloonView(Context context, View content) {
        super(context);
        setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
        setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        setTouchable(true);
        setFocusable(true);
        setOutsideTouchable(true);  
        setTouchInterceptor(new View.OnTouchListener() {
              @Override
              public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                    BaloonView.this.dismiss();                
                  return true;
                }               
                return false;
              }
            });
    }

    public void showUnderView(View view, View content) {
        setBackgroundDrawable(view.getContext().getResources().getDrawable(R.drawable.popup_inline_error_holo_dark));
        FrameLayout container = new FrameLayout(view.getContext());
        container.addView(content);
        setContentView(container);
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        container.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
        int xoffset = view.getWidth() / 2 - container.getMeasuredWidth() / 2;
        showAsDropDown(view, xoffset, 0);
    }
}