3

I have a dialog in an Android app that I don't want the user to be able to cancel. Using .setCancelable(false) disables the back button, but pressing the search button still cancels the dialog. I saw this question which told me that I should include

public boolean onSearchRequested() {
    return false;
}

But I'm still able to cancel the dialog with the search button. Here's my code:

public class TestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        showDialog(0);
    }

    public boolean onSearchRequested() {
        return false;
    }

    protected Dialog onCreateDialog(int id) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Message")
               .setCancelable(false)
               .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // nothing
                   }
               });
         return builder.create();
    }
}
Community
  • 1
  • 1
BenH
  • 2,100
  • 2
  • 22
  • 33
  • This question appears to have been answered: http://stackoverflow.com/questions/2502443/prevent-progressdialog-from-being-dismissed-when-i-click-the-search-button-andr – mah Mar 23 '12 at 00:09

4 Answers4

4

@Benh You need this code to set for your Key Listener for Dialog

   builder.setOnKeyListener(keylistener);

Add Below code in your Activity Class

  OnKeyListener keylistener=new DialogInterface.OnKeyListener() {
    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_SEARCH && event.getRepeatCount() == 0) {
            return true; //we stop begin cancel of dialog or Progressbar
        }
        return false; 
    }
}; 

try this above thing in your dialog hope that will work for you.

Herry
  • 7,037
  • 7
  • 50
  • 80
2

I disable search button by overriding progress dialog. I create unnamed class and override method onSearchRequest() . And this is working for me. I use this:

progressDialog = new ProgressDialog(Activity.this){

                @Override
                public boolean onSearchRequested() {
                    return true;
                }           


            };

instead code:

progressDialog = new ProgressDialog(Activity.this);
mattoke
  • 21
  • 1
2
 public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(event.ACTION_DOWN==KeyEvent.KEYCODE_SEARCH)
        return false;
    else
    return super.onKeyDown(keyCode, event);
}
1

You simply need to listen for search button presses and do nothing when they are hit.

public boolean onKeyDown(int keycode, KeyEvent e) {
            switch(keycode) {
                case KeyEvent.KEYCODE_SEARCH:
                    return true;
                    break;
            }

            return super.onKeyDown(keycode, e);
        }

If this doesn't work for your Activity class then you'll probably need to create a subclass of Dialog and implement the onKeyDown method for your dialog class.

slayton
  • 20,123
  • 10
  • 60
  • 89
  • Thanks, but this isn't working for me. I replaced the `onSearchRequested` function in my code with this code, except that I removed the `break;` statement because Eclipse gave me an unreachable code error. My dialog can still be dismissed by pressing the search button. – BenH Mar 23 '12 at 00:17
  • Hmm.. ok then you probably need to create a subclass of Dialog and override its `onKeyDown` method – slayton Mar 23 '12 at 00:21