0

Based on a YouTube tutorial video (https://www.youtube.com/watch?v=wxqgtEewdfo) that teaches how to create popup windows, I was wondering how to dismiss the popup with a touch event as opposed to the back button...

Here's MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    RelativeLayout relative = (RelativeLayout)findViewById(R.id.relativeTest);

    Button button = (Button)findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext().
                    getSystemService(LAYOUT_INFLATER_SERVICE);
            ViewGroup container = (ViewGroup) layoutInflater.inflate(R.layout.myLayout, null);

            PopupWindow popupWindow = new PopupWindow(container, AbsListView.LayoutParams.MATCH_PARENT,
                    AbsListView.LayoutParams.MATCH_PARENT, true);
            popupWindow.showAtLocation(relative, Gravity.CENTER, 0, 0);

            container.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    popupWindow.dismiss();

                    return true;
                }
            });
        }
    });
}

@Override
public void onBackPressed() {
    // I want the back button to be disabled for both MainActivity and the
    // popup window.
}

... Should I place onBackPressed() elsewhere or would that even be possible?

Thanks in advance.

DaveNOTDavid
  • 1,753
  • 5
  • 19
  • 37
  • You can override onBackPressed and use some logic (for example checking if the popup is showing). Only call super.onBackPressed in situations that you want to actually use backPressed. For example, `if(!mPopupWindow.isShowing()) super.onBackPressed();` – zgc7009 Apr 05 '16 at 16:23
  • Possible duplicate of [Android popup window dismissal](http://stackoverflow.com/questions/3121232/android-popup-window-dismissal) – Wilder Pereira Apr 05 '16 at 16:27
  • @zgc7009 I know what you mean and that's what I did earlier, but I changed gears here... I want to disable the back button in general for both activity_main and the popup (dismissable only by a touch event), and it somehow only works for devices under API 23 (6.0). – DaveNOTDavid Apr 05 '16 at 16:39
  • @WilderPereira Really? I don't think so; the back button dismisses my popups if I want it to... I want the opposite for **all** devices. – DaveNOTDavid Apr 05 '16 at 16:43

3 Answers3

3

Ok, I figured it out (credits to Filip, the uploader of the YouTube video) now... The problem was that I included and set the last parameter of PopupWindow, focusable, to true, and as soon as I got rid of it as follows:

popupWindow = new PopupWindow(container, AbsListView.LayoutParams.MATCH_PARENT,
                    AbsListView.LayoutParams.MATCH_PARENT);

... The popup is now only dismissable by a touch event.

DaveNOTDavid
  • 1,753
  • 5
  • 19
  • 37
0

Just leave onBackPressed blank. Remove the super.OnBackPressed line

Ankesh kumar Jaisansaria
  • 1,563
  • 4
  • 26
  • 44
0

Try this:-

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
if (Integer.parseInt(android.os.Build.VERSION.SDK) > 5
        && keyCode == KeyEvent.KEYCODE_BACK) {
    Log.d("CDA", "onKeyDown Called");
    onBackPressed();
    return true; 
}
return super.onKeyDown(keyCode, event);
}
Ankesh kumar Jaisansaria
  • 1,563
  • 4
  • 26
  • 44