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 sear
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.ACTION_DOWN==KeyEvent.KEYCODE_SEARCH)
return false;
else
return super.onKeyDown(keyCode, event);
}
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);
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.
@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.