Skip to content Skip to sidebar Skip to footer

Disable The Search Button In Android

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 cance

Solution 1:

@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) {
            returntrue; //we stop begin cancel of dialog or Progressbar
        }
        returnfalse; 
    }
}; 

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

Solution 2:

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(event.ACTION_DOWN==KeyEvent.KEYCODE_SEARCH)
        returnfalse;
    elsereturn super.onKeyDown(keyCode, event);
}

Solution 3:

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 = newProgressDialog(Activity.this){

                @OverridepublicbooleanonSearchRequested() {
                    returntrue;
                }           


            };

instead code:

progressDialog = new ProgressDialog(Activity.this);

Solution 4:

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

publicbooleanonKeyDown(int keycode, KeyEvent e) {
            switch(keycode) {
                case KeyEvent.KEYCODE_SEARCH:
                    returntrue;
                    break;
            }

            returnsuper.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.

Post a Comment for "Disable The Search Button In Android"