How to save the search button from closing AlertDialog?

If I create a standalone alertdialog with a builder (not connected to an activity / view), how can I get the search button to cause alertdialog to close?

Thank.

+5
source share
2 answers

It is really complicated, I have a LayoutInflater, but this also needs to be closed. Well, the crappy way to do this is to have a view that you just see visible or invisible.

0
source

I also ran into the same problem showing the EULA dialog. It was resolved by setOnKeyListener.

here is the solution:

                AlertDialog.Builder builder = new AlertDialog.Builder(mActivity)
                    .setTitle(title)
                    .setMessage(message)
                    .setPositiveButton(android.R.string.ok, new Dialog.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            SharedPreferences.Editor editor = prefs.edit();
                            editor.putInt(Constants.EULA_VERSION, versionInfo.versionCode);
                            editor.commit();
                            dialogInterface.dismiss();
                        }
                    })
                    .setNegativeButton(android.R.string.cancel, new Dialog.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // Close the activity once the EULA is declined.
                            mActivity.finish(); 
                        }

                    });

            //To avoid skipping EULA screen through search & menu button.
            builder.setOnKeyListener(new DialogInterface.OnKeyListener() {
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    if (keyCode < KeyEvent.KEYCODE_DPAD_UP || keyCode > KeyEvent.KEYCODE_DPAD_CENTER) 
                    {
                        return true;
                    }
                    else
                        return false;
                }
            });
            builder.create().show();
0
source

All Articles