5

If I create a standalone alertdialog with the builder (not connected to the activity/view) how can I keep the search button from causing the alertdialog to close?

Thanks.

Dave
  • 59
  • 2

2 Answers2

0

That's really tricky, I have LayoutInflater in mind but that thing too needs to close. Well a cheesy way to do that is have a View that you just do visible or invisible

StealthOne
  • 194
  • 9
0

I'm also faced the same problem while showing EULA Dialog. It was solved 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();
Shahul3D
  • 2,129
  • 1
  • 15
  • 16