0

I am writing an application to animate popup window. It was working great with my code.

I want to close the popup window(i.e to slide-down it), when back button is pressed on device.

But I couldn't listen any one key from device. I used setOnKeyListener of that popup window, I didn't even get log from it.

My Code is given below:

popup_layout = layoutInflater.inflate(R.layout.popup_addchannel, null);
            popupWindow = new PopupWindow(popup_layout, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
subscribeButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {

//                  Log.d(TAG,
//                          "Button is clicked for animation....  Visibility is"
//                                  + subscribeButton.getVisibility());
                    openMenu(view);
                }
            });
popup_layout.setOnKeyListener(new View.OnKeyListener() {

                @Override
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    Log.d(TAG, "on key button click called.........");
                    return false;
                }

            });

public void openMenu(View view) {
        if (!flag) {
            popupWindow.setAnimationStyle(R.style.PopupWindowAnimation);
            popupWindow.showAtLocation(view.findViewById(R.id.button1),
                    Gravity.CENTER, 0, 0);
            popupWindow.setFocusable(true);
            popupWindow.update();
            flag = true;
        } else {
            popupWindow.dismiss();
            popupWindow.setFocusable(false);
            flag = false;
        }
    }

What is the problem behind this?

Shall i achieve my requirement?

Please, Guide me.

Thank you in Advance!

Dhasneem
  • 4,037
  • 4
  • 33
  • 47

6 Answers6

3

try this....

    final PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,true);
    ...
    popupWindow.getContentView().setFocusableInTouchMode(true);
    popupMenu.getContentView().setOnKeyListener(new View.OnKeyListener() {        
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode ==  KeyEvent.KEYCODE_MENU && 
                    event.getRepeatCount() == 0 && 
                    event.getAction() == KeyEvent.ACTION_DOWN) {
                // ... payload action here. e.g. popupMenu.dismiss();
                return true;
            }                
            return false;
        }
    });
Android
  • 106
  • 5
  • 1
    After setting "popupWindow.getContentView().setFocusableInTouchMode(true);" this, it was working fine... Thank you for your reply. – Dhasneem Apr 23 '13 at 07:07
  • escuse me Sir, I am having an issue: – Dhasneem Apr 24 '13 at 07:15
  • When Editing the text, if we press the back button, the keyboard will close. If i want to slide down that popup window after editing text, that back button does not work. Please help me to fix this issue. – Dhasneem Apr 24 '13 at 07:18
  • I found out the answer. After editing EditText only will be in focus. So, we need to set "setOnKeyListener" for EditText. In that we have to check for back key pressed as "keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN".. – Dhasneem Apr 24 '13 at 07:46
  • The above is not working. Is there any reason why listener is not registered. Please help – Adi Nov 14 '14 at 06:33
  • got the solution above listenr to listen to key events, popupwindow bacground must be null. – Adi Dec 06 '14 at 11:29
1

The dialog has a property of cancelling the dialog on back press.

dialog.setCancelable(true);

EDIT: Check Qberticus answer on this link: Android popup window dismissal

and you can also see: Issue dismissing popup window

Community
  • 1
  • 1
Shrikant Ballal
  • 7,067
  • 7
  • 41
  • 61
  • But i am using popupWindow na? it doesn't have this property. How can i dismiss it, when i pressed back button? – Dhasneem Apr 23 '13 at 06:33
  • When Editing the text, if we press the back button, the keyboard will close. If i want to slide down that popup window after editing text, that back button does not work. Please help me to fix this issue. – Dhasneem Apr 24 '13 at 07:19
0

You should override the onKeyPressed() method of your activity as below.

@Override
public void onBackPressed() {
   super.onBackPressed();
   //your code to close the popup window.
   popupWindow.setAnimationStyle(R.style.PopupWindowAnimation);
   popupWindow.showAtLocation(view.findViewById(R.id.button1),
            Gravity.CENTER, 0, 0);
   opupWindow.setFocusable(true);
   popupWindow.update();
   flag = true;
}
Kameswari
  • 738
  • 7
  • 27
  • Thank you for your quick reply... I already used onBackPressed for other purpose in this activity. – Dhasneem Apr 23 '13 at 07:08
  • Sorry for wrong information in the last comment.. This class extends from fragment. So i couldn't override onBackPressed in it. I used onBackPressed in other activity.Sorry.. – Dhasneem Apr 23 '13 at 07:22
  • Then, you can put this piece of code in onBackPressed() of FragmentActivity class..right? – Kameswari Apr 23 '13 at 07:26
  • I couldn't... That's the problem.. Then only i decided to use setOnKeyListener in this fragment class.. – Dhasneem Apr 23 '13 at 07:34
  • When Editing the text, if we press the back button, the keyboard will close. If i want to slide down that popup window after editing text, that back button does not work. Please help me to fix this issue. – Dhasneem Apr 24 '13 at 07:20
0

Yes you can use this dialog_obj.setCancelable(true);

Manasi
  • 348
  • 1
  • 4
  • 14
0
popupWindow.getContentView().setOnClickListener(new OnClickListener()) {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        popupWindow.dismiss();
    }
});

Try this.

JBL
  • 12,588
  • 4
  • 53
  • 84
rohan
  • 1
0

You can't listen a key press in popup window, but when you listen to system back key, you can override dismiss() method to intercept it.

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
Pinnh
  • 1