1

I'm creating a custom soft keyboard, and created a PopupWindow to show when a key is long pressed, like when you long press E and it shows E, É, È for you to choose one. The popup has a key to close him, but I want to remove this key and make him shows just while the user is touching, then the user long press, drag to the key that he want and release.

I'm using android API 8.

The popup is created in a KeyboardView class in the onLongPress method.

final View custom = LayoutInflater.from(context)
     .inflate(R.layout.popup_layout, new FrameLayout(context));
final PopupWindow popup = new PopupWindow(context);

popup.setContentView(custom);

        popup.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
        popup.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        popup.showAtLocation(this, Gravity.NO_GRAVITY, popupKey.x, popupKey.y-50);

The button for close the popup:

        buttonCancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                popup.dismiss();
            }
        });

I think can use something with the onTouch method, but how to identify the event of releasing? And where use it? On the keyboardView, or maybe on the popup window?

Ricardo A.
  • 685
  • 2
  • 8
  • 35

2 Answers2

0

I managed to do this with:

@Override
public boolean onTouchEvent(MotionEvent me){
    if(popup != null && me.getAction() == MotionEvent.ACTION_UP){
        popup.dismiss();
    }
}

I first created a method to show on logcat the code of every touch event, then I got the code that appeared when I release the touch and compared with the documentation, and it it the code for MotionEvent.ADCTION_UP. With this, it's just put a dismiss on the popup.

Ricardo A.
  • 685
  • 2
  • 8
  • 35
0

I saw that there is a private function called KeyboardView.dismissPopupKeyboard().

This is ugly, but because I am using the default PopupWindow (I didn't overrided OnLongPress()) and I saw thata KeyboardView.onClick() calls dismissPopupKeyboard() and that's it, what I did was:

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        if (event.getAction() == MotionEvent.ACTION_UP){
            getOnKeyboardActionListener().onKey('1', null);
            dismissPopupKeyboard();
        }
        return true; // Required for recieving subsequent events (ACTION_MOVE, ACTION_UP)
    }
Amit Klein
  • 124
  • 1
  • 7