Android: callback for search widget opened and dismissed

I have a screen layout that is forced to be potrait mode. Because it is very complex I don't have the time right now to invest creating a separate one for landscape mode. It also doesn't make much sense for my type of application.

However, for input fields it's better to provide a landscape mode, because some phones have a hardware keyboard, which automatically aligns the phone in landscape and therefor makes it hard to look at the app that is still in portrait mode.

My solution to this is to put all text input into a dialog and temporarily enable landscape mode (if requested by the user) until the dialog is dismissed again.

This works perfectly. Except of the overlaying search widget (when pressing the search button from my application). I'm looking for two callbacks: one, when the search widget is raised (I cannot listen to the search button, because I sometimes raise it manually via a soft button) and when it is dismissed again (regardless if the search was finally triggered or canceled - it needs to work for both cases).

Any suggestions?

Asked by: Ada746 | Posted: 24-01-2022

Answer 1

There is an Activity callback for when search is activated. onSearchRequested()

For the dismiss/cancel of the search widget, you can add listeners via the SearchManager:

Get a reference to your SearchManager with:


see getSystemService()

Answered by: Rafael666 | Posted: 25-02-2022

Answer 2

When using the Search Widget you can use the OnActionExpandListener on the associated action bars menu item. This also works great with the AppCompat Support Library for API versions below 14.

The OnActionExpandListener has two methods:

  • onMenuItemActionCollapse
  • onMenuItemActionExpand

See some code example below:

SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);

    MenuItem searchItem = menu.findItem(;

    MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener(){

        public boolean onMenuItemActionCollapse(MenuItem item) {
            KLog.i(TAG, "onMenuItemActionCollapse");
            return true;

        public boolean onMenuItemActionExpand(MenuItem item) {
            KLog.i(TAG, "onMenuItemActionExpand");
            return true;


    mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);


In case you do not use the Support Library use the OnActionExpandListener on the menu item directly.

Answered by: Max560 | Posted: 25-02-2022

Similar questions

Progress dialog getting dismissed before the thread gets finished - Android

I use the code provided by Fedor in the following link, in order to get the latitude and longitude from my simple demo app. I am trying to fetch the latitude and longitude using the MyLocation class provided by him in that link. What is the simplest and most robust w...

android - Dialog being dismissed when activity restarts

When I rotate the device when a dialog is shown, my activity is recreated and the dialog gets dismissed. Do I need to show the dialog again each time the user rotates the device?

android - how to check if the toast have dismissed or not

This question already has answers here:

class - Can you fire an event when Android Dialog is dismissed?

Say I have a created a dialog in my Android app like so: private static ProgressDialog dialog; dialog =, "", "Downloading Files. Please wait...", true); Now, is it possible to fire an event when the following is called? dialog.dismiss(); The reason I want to do this and not just call my method after dialog.dismis...

android - Dismissed dialog is shown when returning to Actitiy,

I have actity wich raises an alert dialog. Next, I dismiss the dialog and start a new Activity using an Intent. The dialog is dismissed, dialog's onStop and onDismissDialog Callbacks are notified, and the new activity is open. Problem happens when I finish the second activity (either by calling Activity.finigh() or kill the proccess with adb). The first activity is shown again BUT the dialog is still there.

android - Dialog does not open when called shortly after being dismissed

In my app, the user logs in with a custom made login dialog. The user can confirm and exit the dialog in two ways: Press the Enter/Done button in the password box. Press the OK button. When the user has confirmed, the provided credentials are verified. If the credentials were incorrect, the dialog will reappear. This does only work if the user presses the Enter/Done button in the passw...

android - Getting notified when virtual keyboard shown / dismissed for an EditText?

I'd like to get notified when the virtual keyboard is shown / dismissed. This does not seem to be possible, other than by using some layout resizing listener tricks: How to check visibility of software keyboard in Android? My activity has a single EditText. I could make it not have focus at activity startup...

android - call a function, after a dialog box gets dismissed

in my app, when the activity gets launched i am calling an AsynTask to load some details in the page. In between if the user clicks a button, an alert dialog builder used to appear. When the user clicks the Ok button i want the dialog to be dismissed and again i want to call the async task. how to do this. i tried calling the function within the alert box before dismissing the dialog box it gets crashed. a...

android - Custom Dialog is being dismissed when I click outside of it

I have a custom dialog that dismisses when you click outside of the dialog, which is what I do not want. Having dialog.setCanceledOnTouchOutside(false); does not fix the problem. What am I doing wrong? dialog = new Dialog(context); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setCanceledOnTouchOutside(false); dialog.setContentView(R.layout.twitter_dialog); // set up edit text ...

android - Dismissed dialog shows up again after screen unlock

i observed already a few times that an already dismissed dialog shows up again after the phone is a while locked and then gets unlocked. this happens once in a while, not always. the code that i use is pretty straight forward: showDialog(DIALOG_LOADING); new AsyncTask<Void, Void, PhotoList>(){ @Override protected PhotoList doInBackground(Void... params) { // load and return dat...

Still can't find your answer? Check out these communities...

Android Google Support | Android Community | Android Community (Facebook) | Android