Close Android popup window with back press

后端 未结 8 2022
春和景丽
春和景丽 2021-01-05 02:42

I have created a android application where I have created a popup screen. But when I am pressing the back button, popup is not getting closed.

I have tried with onB

相关标签:
8条回答
  • 2021-01-05 02:57
    LayoutInflater layoutInflater = (LayoutInflater)MainActivity.this.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        View popupView = layoutInflater.inflate(R.layout.popup_window_country_list, null);
        countryPopup = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    
        countryPopup.setBackgroundDrawable (new BitmapDrawable());
    
        countryPopup.setFocusable(true); //Make Here True For back press dismiss
    
        countryPopup.setOutsideTouchable(true); 
    
        countryPopup.setTouchInterceptor(new OnTouchListener() {
    
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
    
                    countryPopup.dismiss();
    
    
                    return true;
    
                }
    
                return false;
    
            }
        });
    
    0 讨论(0)
  • 2021-01-05 02:59

    The popup window normally closes when you press the back button. if you need to handle certain things when the popup closes, you can use

    popupWindow.setOnDismissListener
    

    Note:- I worked in kotlin. It worked for me

    0 讨论(0)
  • 2021-01-05 03:00
    popup.setBackgroundDrawable(new BitmapDrawable());
    
    popup.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    //do your code here
                }
            });
    
    0 讨论(0)
  • 2021-01-05 03:01

    What you need to do is call setBackgroundDrawable on your PopupWindow after you initialize it. Something like:

    myPopup.setBackgroundDrawable(new BitmapDrawable());
    
    0 讨论(0)
  • 2021-01-05 03:03

    Following code is working perfectly. So override following function in your activity

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
         //Changes 'back' button action
        if(keyCode==KeyEvent.KEYCODE_BACK)
        {
            if(!popUpHelper.isPopupShowing()){
                onBackPressed();
            }else{
                popUpHelper.dismiss();
            }
        }
    
        return super.onKeyDown(keyCode, event);
    }
    
    
    class PopupHelper {
     PopupWindow popupWindowAttachment;
      public void initAndShow(Activity context,int mToolbarHeight){
    
       View layout = activity.getLayoutInflater().inflate(
                R.layout.activity_chat_attachment_popup, null);
        //create and show and Make sure popup window focusable should be false
        popupWindowAttachment = new PopupWindow(layout,
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindowAttachment.showAsDropDown(layout, 0, mToolbarHeight
                + (mToolbarHeight / 2) - 5);
    
      }
    
      public void dismiss() {
        if (isPopupShowing())
                popupWindowAttachment.dismiss();
      }
      public boolean isPopupShowing() {
        return popupWindowAttachment==null?false:popupWindowAttachment  
                     .isShowing();
      }
    }
    
    0 讨论(0)
  • 2021-01-05 03:08

    Recently I worked with ListPopupWindow (android.support.v7.internal.widget.ListPopupWindow) and back button started to work when I called

    popupWindow.setModal(true);
    

    no matter what I set in setBackgroundDrawable method as other solutions suppose here.

    0 讨论(0)
提交回复
热议问题