MenuItemCompat.setOnActionExpandListener doesn't do anything












10














Background



I have a searchView being initialized using a special class I've made, that's being used across all of the activities and fragments.



The problem



Recently, probably due to updates to the support library (or because I didn't use it so far, I don't remember), I can't catch events of expand/collapse of the searchView.



As I've found, this happens even if I use setSupportActionBar with a Toolbar instance.



What I've tried



I've tried using each of the next methods, but none worked:




  • MenuItemCompat.setOnActionExpandListener.

  • MenuItemCompat.setOnActionExpandListener together with iconifying the SearchView, as suggested on some websites.

  • setOnActionExpandListener on the search menu item itself, but then it crashes since it can't be used when extending the ActionBarActivity.

  • SearchView.setOnCloseListener , but this works only if I close it, and only using the UI (doesn't get called when calling collapseActionView ).

  • I've also tried to mess around with the XML file of the search menu item.


The code



Here's the helper class I've made:



SearchHolderCompat



public class SearchHolderCompat {
public MenuItem mSearchMenuItem;
public SearchView mSearchView;
private final Activity _context;

public SearchHolderCompat(final Activity context) {
_context = context;
}

public boolean isCurrentyExpanded() {
return mSearchMenuItem != null && MenuItemCompat.isActionViewExpanded(mSearchMenuItem);
}

public boolean hasQuery() {
return mSearchMenuItem != null && mSearchView != null && MenuItemCompat.isActionViewExpanded(mSearchMenuItem)
&& !TextUtils.isEmpty(mSearchView.getQuery());
}

public void addSearchItemAndInit(final Menu menu, final OnQueryTextListener onQueryTextListener,
final OnActionExpandListener onActionExpandListener) {
final MenuInflater menuInflater = _context.getMenuInflater();
menuInflater.inflate(R.menu.search_menu_item, menu);
init(menu.findItem(R.id.menuItem_search), onQueryTextListener, onActionExpandListener);
}

public void init(final MenuItem searchMenuItem, final OnQueryTextListener onQueryTextListener,
final OnActionExpandListener onActionExpandListener) {
this.mSearchMenuItem = searchMenuItem;
mSearchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
if (mSearchView == null) {
MenuItemCompat.setShowAsAction(searchMenuItem, MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
| MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
MenuItemCompat.setActionView(searchMenuItem, mSearchView = new SearchView(_context));
}
mSearchView.setQueryHint(_context.getString(R.string.search));
mSearchView.setOnQueryTextListener(onQueryTextListener);
MenuItemCompat.setOnActionExpandListener(searchMenuItem, onActionExpandListener);
}

}


MainActivity.java



public class MainActivity extends ActionBarActivity {
SearchHolderCompat mSearchHolder = new SearchHolderCompat(this);

@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
mSearchHolder.addSearchItemAndInit(menu, new OnQueryTextListener() {

@Override
public boolean onQueryTextSubmit(final String arg0) {
android.util.Log.d("AppLog", "onQueryTextSubmit");
return false;
}

@Override
public boolean onQueryTextChange(final String queryText) {
android.util.Log.d("AppLog", "onQueryTextChange");
return true;
}
}, new OnActionExpandListener() {

@Override
public boolean onMenuItemActionExpand(final MenuItem arg0) {
android.util.Log.d("AppLog", "onMenuItemActionExpand");
return false;
}

@Override
public boolean onMenuItemActionCollapse(final MenuItem arg0) {
android.util.Log.d("AppLog", "onMenuItemActionCollapse");
return false;
}
});
return true;
}
}


search_menu_item.xml



<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" >

<!-- search items -->
<item
android:id="@+id/menuItem_search"
android:icon="@drawable/ic_action_search"
android:title="@string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always"
tools:ignore="AlwaysShowAction"/>

</menu>


The question



What's the correct way to handle the SearchView and the search menu item (using the support library) ?



How come "MenuItemCompat.setOnActionExpandListener" doesn't work?










share|improve this question






















  • whats your device api level?
    – pskink
    Dec 14 '14 at 12:38










  • also is your MenuItem item an instance of android.support.v4.internal.view.SupportMenuItem ? see: androidxref.com/5.0.0_r2/xref/frameworks/support/v4/java/…
    – pskink
    Dec 14 '14 at 12:46








  • 1




    @pskink SupportMenuItem is an interface. You can't create a new instance of it... About device API level, it's using the support library v7, so it should work from API7. However, the app's minSdk is 14 (ICS). I'd like to know the solution for all versions though. The official solution for the support library. The reason is that ActionBarActivity provides more than just supporting pre-Honeycomb devices.
    – android developer
    Dec 14 '14 at 12:50








  • 1




    @pskink I don't create it. It's the framework that's responsible of doing it. I just pass it using "menu.findItem" . Even if you add it by yourself, you get the MenuItem of Android framework.
    – android developer
    Dec 14 '14 at 12:53






  • 1




    @pskink This is the class of the menu item : "android.support.v7.internal.view.menu.MenuItemImpl" .
    – android developer
    Dec 14 '14 at 12:56
















10














Background



I have a searchView being initialized using a special class I've made, that's being used across all of the activities and fragments.



The problem



Recently, probably due to updates to the support library (or because I didn't use it so far, I don't remember), I can't catch events of expand/collapse of the searchView.



As I've found, this happens even if I use setSupportActionBar with a Toolbar instance.



What I've tried



I've tried using each of the next methods, but none worked:




  • MenuItemCompat.setOnActionExpandListener.

  • MenuItemCompat.setOnActionExpandListener together with iconifying the SearchView, as suggested on some websites.

  • setOnActionExpandListener on the search menu item itself, but then it crashes since it can't be used when extending the ActionBarActivity.

  • SearchView.setOnCloseListener , but this works only if I close it, and only using the UI (doesn't get called when calling collapseActionView ).

  • I've also tried to mess around with the XML file of the search menu item.


The code



Here's the helper class I've made:



SearchHolderCompat



public class SearchHolderCompat {
public MenuItem mSearchMenuItem;
public SearchView mSearchView;
private final Activity _context;

public SearchHolderCompat(final Activity context) {
_context = context;
}

public boolean isCurrentyExpanded() {
return mSearchMenuItem != null && MenuItemCompat.isActionViewExpanded(mSearchMenuItem);
}

public boolean hasQuery() {
return mSearchMenuItem != null && mSearchView != null && MenuItemCompat.isActionViewExpanded(mSearchMenuItem)
&& !TextUtils.isEmpty(mSearchView.getQuery());
}

public void addSearchItemAndInit(final Menu menu, final OnQueryTextListener onQueryTextListener,
final OnActionExpandListener onActionExpandListener) {
final MenuInflater menuInflater = _context.getMenuInflater();
menuInflater.inflate(R.menu.search_menu_item, menu);
init(menu.findItem(R.id.menuItem_search), onQueryTextListener, onActionExpandListener);
}

public void init(final MenuItem searchMenuItem, final OnQueryTextListener onQueryTextListener,
final OnActionExpandListener onActionExpandListener) {
this.mSearchMenuItem = searchMenuItem;
mSearchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
if (mSearchView == null) {
MenuItemCompat.setShowAsAction(searchMenuItem, MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
| MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
MenuItemCompat.setActionView(searchMenuItem, mSearchView = new SearchView(_context));
}
mSearchView.setQueryHint(_context.getString(R.string.search));
mSearchView.setOnQueryTextListener(onQueryTextListener);
MenuItemCompat.setOnActionExpandListener(searchMenuItem, onActionExpandListener);
}

}


MainActivity.java



public class MainActivity extends ActionBarActivity {
SearchHolderCompat mSearchHolder = new SearchHolderCompat(this);

@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
mSearchHolder.addSearchItemAndInit(menu, new OnQueryTextListener() {

@Override
public boolean onQueryTextSubmit(final String arg0) {
android.util.Log.d("AppLog", "onQueryTextSubmit");
return false;
}

@Override
public boolean onQueryTextChange(final String queryText) {
android.util.Log.d("AppLog", "onQueryTextChange");
return true;
}
}, new OnActionExpandListener() {

@Override
public boolean onMenuItemActionExpand(final MenuItem arg0) {
android.util.Log.d("AppLog", "onMenuItemActionExpand");
return false;
}

@Override
public boolean onMenuItemActionCollapse(final MenuItem arg0) {
android.util.Log.d("AppLog", "onMenuItemActionCollapse");
return false;
}
});
return true;
}
}


search_menu_item.xml



<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" >

<!-- search items -->
<item
android:id="@+id/menuItem_search"
android:icon="@drawable/ic_action_search"
android:title="@string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always"
tools:ignore="AlwaysShowAction"/>

</menu>


The question



What's the correct way to handle the SearchView and the search menu item (using the support library) ?



How come "MenuItemCompat.setOnActionExpandListener" doesn't work?










share|improve this question






















  • whats your device api level?
    – pskink
    Dec 14 '14 at 12:38










  • also is your MenuItem item an instance of android.support.v4.internal.view.SupportMenuItem ? see: androidxref.com/5.0.0_r2/xref/frameworks/support/v4/java/…
    – pskink
    Dec 14 '14 at 12:46








  • 1




    @pskink SupportMenuItem is an interface. You can't create a new instance of it... About device API level, it's using the support library v7, so it should work from API7. However, the app's minSdk is 14 (ICS). I'd like to know the solution for all versions though. The official solution for the support library. The reason is that ActionBarActivity provides more than just supporting pre-Honeycomb devices.
    – android developer
    Dec 14 '14 at 12:50








  • 1




    @pskink I don't create it. It's the framework that's responsible of doing it. I just pass it using "menu.findItem" . Even if you add it by yourself, you get the MenuItem of Android framework.
    – android developer
    Dec 14 '14 at 12:53






  • 1




    @pskink This is the class of the menu item : "android.support.v7.internal.view.menu.MenuItemImpl" .
    – android developer
    Dec 14 '14 at 12:56














10












10








10


1





Background



I have a searchView being initialized using a special class I've made, that's being used across all of the activities and fragments.



The problem



Recently, probably due to updates to the support library (or because I didn't use it so far, I don't remember), I can't catch events of expand/collapse of the searchView.



As I've found, this happens even if I use setSupportActionBar with a Toolbar instance.



What I've tried



I've tried using each of the next methods, but none worked:




  • MenuItemCompat.setOnActionExpandListener.

  • MenuItemCompat.setOnActionExpandListener together with iconifying the SearchView, as suggested on some websites.

  • setOnActionExpandListener on the search menu item itself, but then it crashes since it can't be used when extending the ActionBarActivity.

  • SearchView.setOnCloseListener , but this works only if I close it, and only using the UI (doesn't get called when calling collapseActionView ).

  • I've also tried to mess around with the XML file of the search menu item.


The code



Here's the helper class I've made:



SearchHolderCompat



public class SearchHolderCompat {
public MenuItem mSearchMenuItem;
public SearchView mSearchView;
private final Activity _context;

public SearchHolderCompat(final Activity context) {
_context = context;
}

public boolean isCurrentyExpanded() {
return mSearchMenuItem != null && MenuItemCompat.isActionViewExpanded(mSearchMenuItem);
}

public boolean hasQuery() {
return mSearchMenuItem != null && mSearchView != null && MenuItemCompat.isActionViewExpanded(mSearchMenuItem)
&& !TextUtils.isEmpty(mSearchView.getQuery());
}

public void addSearchItemAndInit(final Menu menu, final OnQueryTextListener onQueryTextListener,
final OnActionExpandListener onActionExpandListener) {
final MenuInflater menuInflater = _context.getMenuInflater();
menuInflater.inflate(R.menu.search_menu_item, menu);
init(menu.findItem(R.id.menuItem_search), onQueryTextListener, onActionExpandListener);
}

public void init(final MenuItem searchMenuItem, final OnQueryTextListener onQueryTextListener,
final OnActionExpandListener onActionExpandListener) {
this.mSearchMenuItem = searchMenuItem;
mSearchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
if (mSearchView == null) {
MenuItemCompat.setShowAsAction(searchMenuItem, MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
| MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
MenuItemCompat.setActionView(searchMenuItem, mSearchView = new SearchView(_context));
}
mSearchView.setQueryHint(_context.getString(R.string.search));
mSearchView.setOnQueryTextListener(onQueryTextListener);
MenuItemCompat.setOnActionExpandListener(searchMenuItem, onActionExpandListener);
}

}


MainActivity.java



public class MainActivity extends ActionBarActivity {
SearchHolderCompat mSearchHolder = new SearchHolderCompat(this);

@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
mSearchHolder.addSearchItemAndInit(menu, new OnQueryTextListener() {

@Override
public boolean onQueryTextSubmit(final String arg0) {
android.util.Log.d("AppLog", "onQueryTextSubmit");
return false;
}

@Override
public boolean onQueryTextChange(final String queryText) {
android.util.Log.d("AppLog", "onQueryTextChange");
return true;
}
}, new OnActionExpandListener() {

@Override
public boolean onMenuItemActionExpand(final MenuItem arg0) {
android.util.Log.d("AppLog", "onMenuItemActionExpand");
return false;
}

@Override
public boolean onMenuItemActionCollapse(final MenuItem arg0) {
android.util.Log.d("AppLog", "onMenuItemActionCollapse");
return false;
}
});
return true;
}
}


search_menu_item.xml



<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" >

<!-- search items -->
<item
android:id="@+id/menuItem_search"
android:icon="@drawable/ic_action_search"
android:title="@string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always"
tools:ignore="AlwaysShowAction"/>

</menu>


The question



What's the correct way to handle the SearchView and the search menu item (using the support library) ?



How come "MenuItemCompat.setOnActionExpandListener" doesn't work?










share|improve this question













Background



I have a searchView being initialized using a special class I've made, that's being used across all of the activities and fragments.



The problem



Recently, probably due to updates to the support library (or because I didn't use it so far, I don't remember), I can't catch events of expand/collapse of the searchView.



As I've found, this happens even if I use setSupportActionBar with a Toolbar instance.



What I've tried



I've tried using each of the next methods, but none worked:




  • MenuItemCompat.setOnActionExpandListener.

  • MenuItemCompat.setOnActionExpandListener together with iconifying the SearchView, as suggested on some websites.

  • setOnActionExpandListener on the search menu item itself, but then it crashes since it can't be used when extending the ActionBarActivity.

  • SearchView.setOnCloseListener , but this works only if I close it, and only using the UI (doesn't get called when calling collapseActionView ).

  • I've also tried to mess around with the XML file of the search menu item.


The code



Here's the helper class I've made:



SearchHolderCompat



public class SearchHolderCompat {
public MenuItem mSearchMenuItem;
public SearchView mSearchView;
private final Activity _context;

public SearchHolderCompat(final Activity context) {
_context = context;
}

public boolean isCurrentyExpanded() {
return mSearchMenuItem != null && MenuItemCompat.isActionViewExpanded(mSearchMenuItem);
}

public boolean hasQuery() {
return mSearchMenuItem != null && mSearchView != null && MenuItemCompat.isActionViewExpanded(mSearchMenuItem)
&& !TextUtils.isEmpty(mSearchView.getQuery());
}

public void addSearchItemAndInit(final Menu menu, final OnQueryTextListener onQueryTextListener,
final OnActionExpandListener onActionExpandListener) {
final MenuInflater menuInflater = _context.getMenuInflater();
menuInflater.inflate(R.menu.search_menu_item, menu);
init(menu.findItem(R.id.menuItem_search), onQueryTextListener, onActionExpandListener);
}

public void init(final MenuItem searchMenuItem, final OnQueryTextListener onQueryTextListener,
final OnActionExpandListener onActionExpandListener) {
this.mSearchMenuItem = searchMenuItem;
mSearchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
if (mSearchView == null) {
MenuItemCompat.setShowAsAction(searchMenuItem, MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
| MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
MenuItemCompat.setActionView(searchMenuItem, mSearchView = new SearchView(_context));
}
mSearchView.setQueryHint(_context.getString(R.string.search));
mSearchView.setOnQueryTextListener(onQueryTextListener);
MenuItemCompat.setOnActionExpandListener(searchMenuItem, onActionExpandListener);
}

}


MainActivity.java



public class MainActivity extends ActionBarActivity {
SearchHolderCompat mSearchHolder = new SearchHolderCompat(this);

@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
mSearchHolder.addSearchItemAndInit(menu, new OnQueryTextListener() {

@Override
public boolean onQueryTextSubmit(final String arg0) {
android.util.Log.d("AppLog", "onQueryTextSubmit");
return false;
}

@Override
public boolean onQueryTextChange(final String queryText) {
android.util.Log.d("AppLog", "onQueryTextChange");
return true;
}
}, new OnActionExpandListener() {

@Override
public boolean onMenuItemActionExpand(final MenuItem arg0) {
android.util.Log.d("AppLog", "onMenuItemActionExpand");
return false;
}

@Override
public boolean onMenuItemActionCollapse(final MenuItem arg0) {
android.util.Log.d("AppLog", "onMenuItemActionCollapse");
return false;
}
});
return true;
}
}


search_menu_item.xml



<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" >

<!-- search items -->
<item
android:id="@+id/menuItem_search"
android:icon="@drawable/ic_action_search"
android:title="@string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always"
tools:ignore="AlwaysShowAction"/>

</menu>


The question



What's the correct way to handle the SearchView and the search menu item (using the support library) ?



How come "MenuItemCompat.setOnActionExpandListener" doesn't work?







android android-actionbar android-support-library searchview






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 14 '14 at 12:21









android developer

55.3k96461849




55.3k96461849












  • whats your device api level?
    – pskink
    Dec 14 '14 at 12:38










  • also is your MenuItem item an instance of android.support.v4.internal.view.SupportMenuItem ? see: androidxref.com/5.0.0_r2/xref/frameworks/support/v4/java/…
    – pskink
    Dec 14 '14 at 12:46








  • 1




    @pskink SupportMenuItem is an interface. You can't create a new instance of it... About device API level, it's using the support library v7, so it should work from API7. However, the app's minSdk is 14 (ICS). I'd like to know the solution for all versions though. The official solution for the support library. The reason is that ActionBarActivity provides more than just supporting pre-Honeycomb devices.
    – android developer
    Dec 14 '14 at 12:50








  • 1




    @pskink I don't create it. It's the framework that's responsible of doing it. I just pass it using "menu.findItem" . Even if you add it by yourself, you get the MenuItem of Android framework.
    – android developer
    Dec 14 '14 at 12:53






  • 1




    @pskink This is the class of the menu item : "android.support.v7.internal.view.menu.MenuItemImpl" .
    – android developer
    Dec 14 '14 at 12:56


















  • whats your device api level?
    – pskink
    Dec 14 '14 at 12:38










  • also is your MenuItem item an instance of android.support.v4.internal.view.SupportMenuItem ? see: androidxref.com/5.0.0_r2/xref/frameworks/support/v4/java/…
    – pskink
    Dec 14 '14 at 12:46








  • 1




    @pskink SupportMenuItem is an interface. You can't create a new instance of it... About device API level, it's using the support library v7, so it should work from API7. However, the app's minSdk is 14 (ICS). I'd like to know the solution for all versions though. The official solution for the support library. The reason is that ActionBarActivity provides more than just supporting pre-Honeycomb devices.
    – android developer
    Dec 14 '14 at 12:50








  • 1




    @pskink I don't create it. It's the framework that's responsible of doing it. I just pass it using "menu.findItem" . Even if you add it by yourself, you get the MenuItem of Android framework.
    – android developer
    Dec 14 '14 at 12:53






  • 1




    @pskink This is the class of the menu item : "android.support.v7.internal.view.menu.MenuItemImpl" .
    – android developer
    Dec 14 '14 at 12:56
















whats your device api level?
– pskink
Dec 14 '14 at 12:38




whats your device api level?
– pskink
Dec 14 '14 at 12:38












also is your MenuItem item an instance of android.support.v4.internal.view.SupportMenuItem ? see: androidxref.com/5.0.0_r2/xref/frameworks/support/v4/java/…
– pskink
Dec 14 '14 at 12:46






also is your MenuItem item an instance of android.support.v4.internal.view.SupportMenuItem ? see: androidxref.com/5.0.0_r2/xref/frameworks/support/v4/java/…
– pskink
Dec 14 '14 at 12:46






1




1




@pskink SupportMenuItem is an interface. You can't create a new instance of it... About device API level, it's using the support library v7, so it should work from API7. However, the app's minSdk is 14 (ICS). I'd like to know the solution for all versions though. The official solution for the support library. The reason is that ActionBarActivity provides more than just supporting pre-Honeycomb devices.
– android developer
Dec 14 '14 at 12:50






@pskink SupportMenuItem is an interface. You can't create a new instance of it... About device API level, it's using the support library v7, so it should work from API7. However, the app's minSdk is 14 (ICS). I'd like to know the solution for all versions though. The official solution for the support library. The reason is that ActionBarActivity provides more than just supporting pre-Honeycomb devices.
– android developer
Dec 14 '14 at 12:50






1




1




@pskink I don't create it. It's the framework that's responsible of doing it. I just pass it using "menu.findItem" . Even if you add it by yourself, you get the MenuItem of Android framework.
– android developer
Dec 14 '14 at 12:53




@pskink I don't create it. It's the framework that's responsible of doing it. I just pass it using "menu.findItem" . Even if you add it by yourself, you get the MenuItem of Android framework.
– android developer
Dec 14 '14 at 12:53




1




1




@pskink This is the class of the menu item : "android.support.v7.internal.view.menu.MenuItemImpl" .
– android developer
Dec 14 '14 at 12:56




@pskink This is the class of the menu item : "android.support.v7.internal.view.menu.MenuItemImpl" .
– android developer
Dec 14 '14 at 12:56












4 Answers
4






active

oldest

votes


















5














After looking for a solution for couple of hours I've implement something like this. and worked for me. (I wanted Expand and Collapse events anyhow)

.



@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);



if(searchItem != null)
{

SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
// use this method for search process
searchView.setOnSearchClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//Search view is expanded
showSearchPage();
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener()
{
@Override
public boolean onClose()
{
//Search View is collapsed
hideSearchPage();
return false;
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
{

@Override
public boolean onQueryTextSubmit(String query)
{
// use this method when query submitted
Toast.makeText(MainActivity.this, query, Toast.LENGTH_SHORT).show();
return false;
}

@Override
public boolean onQueryTextChange(String newText)
{
// use this method for auto complete search process
Log.e("SearchValueIs",":"+newText);
return false;
}
});
}
return super.onCreateOptionsMenu(menu);
}




Hope It will help Someone...






share|improve this answer





























    5














    Android training



    app:showAsAction="ifRoom|collapseActionView"





    share|improve this answer





























      1














      You can add ViewTreeObserver to track the visibility state of android.support.v7.appcompat.R.id.search_edit_frame. You can check my answer here: https://stackoverflow.com/a/28762632/1633609



      You can see below how to handle Expand and Collapsed states!



      To answer why MenuItemCompat.setOnActionExpandListener(...) is not working, this listener is only called if the showAsAction of the SearchView is set to MenuItemCompat.SHOW_AS_ACTION_ALWAYS (you can also add more options).



      This is the copy of my answer form the other question:



      I found that MenuItemCompat.setOnActionExpandListener(...) is not working if you don't pass:



          searchItem
      .setShowAsAction(MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
      | MenuItemCompat.SHOW_AS_ACTION_ALWAYS);


      But this is changing the SearchView and is replacing the DrawerToggle with back arrow.



      I wanted to keep the original views and still track the Expanded/Collapsed state and use supported Search View.



      Solution:



      When android.support.v7.widget.SearchView is changing the view state the LinearLayout view's, with id android.support.v7.appcompat.R.id.search_edit_frame, visibility value is being changed from View.VISIBLE to View.GONE and opposite. So I add ViewTreeObserver to track the visibility change of the search edit frame.



      menu_search.xml:



      <menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto" >

      <item
      android:id="@+id/action_search"
      android:icon="@android:drawable/ic_menu_search"
      android:title="@string/search"
      app:actionViewClass="android.support.v7.widget.SearchView"
      app:showAsAction="always"/>

      </menu>


      In the activity:



      import android.support.v4.view.MenuItemCompat;
      import android.support.v7.widget.SearchView;
      import android.view.Menu;
      import android.view.MenuItem;

      ..........

      private View mSearchEditFrame;

      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.menu_search, menu);
      MenuItem searchItem = (MenuItem) menu.findItem(R.id.action_search);

      SearchView searchView = (SearchView) MenuItemCompat
      .getActionView(searchItem);
      searchView.setSubmitButtonEnabled(false);
      mSearchEditFrame = searchView
      .findViewById(android.support.v7.appcompat.R.id.search_edit_frame);

      ViewTreeObserver vto = mSearchEditFrame.getViewTreeObserver();
      vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
      int oldVisibility = -1;

      @Override
      public void onGlobalLayout() {

      int currentVisibility = mSearchEditFrame.getVisibility();

      if (currentVisibility != oldVisibility) {
      if (currentVisibility == View.VISIBLE) {
      Log.v(TAG, "EXPANDED");
      } else {
      Log.v(TAG, "COLLAPSED");
      }

      oldVisibility = currentVisibility;
      }

      }
      });

      return super.onCreateOptionsMenu(menu);
      }





      share|improve this answer































        0














        I know I'm very late to post this answer but hope it helps someone else. I recently came across the issue and I just made the Override methods return true and it worked like a charm.



                @Override
        public boolean onMenuItemActionExpand(final MenuItem arg0) {

        android.util.Log.d("AppLog", "onMenuItemActionExpand");
        return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(final MenuItem arg0) {

        android.util.Log.d("AppLog", "onMenuItemActionCollapse");
        return true;
        }


        In more recent versions



              menuSearch.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {

        //SearchView appers
        return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {

        //SearchView disappears
        return true;
        }
        });





        share|improve this answer





















          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f27469236%2fmenuitemcompat-setonactionexpandlistener-doesnt-do-anything%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          5














          After looking for a solution for couple of hours I've implement something like this. and worked for me. (I wanted Expand and Collapse events anyhow)

          .



          @Override
          public boolean onCreateOptionsMenu(Menu menu)
          {
          getMenuInflater().inflate(R.menu.menu_main, menu);
          MenuItem searchItem = menu.findItem(R.id.action_search);



          if(searchItem != null)
          {

          SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
          // use this method for search process
          searchView.setOnSearchClickListener(new View.OnClickListener()
          {
          @Override
          public void onClick(View v)
          {
          //Search view is expanded
          showSearchPage();
          }
          });
          searchView.setOnCloseListener(new SearchView.OnCloseListener()
          {
          @Override
          public boolean onClose()
          {
          //Search View is collapsed
          hideSearchPage();
          return false;
          }
          });
          searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
          {

          @Override
          public boolean onQueryTextSubmit(String query)
          {
          // use this method when query submitted
          Toast.makeText(MainActivity.this, query, Toast.LENGTH_SHORT).show();
          return false;
          }

          @Override
          public boolean onQueryTextChange(String newText)
          {
          // use this method for auto complete search process
          Log.e("SearchValueIs",":"+newText);
          return false;
          }
          });
          }
          return super.onCreateOptionsMenu(menu);
          }




          Hope It will help Someone...






          share|improve this answer


























            5














            After looking for a solution for couple of hours I've implement something like this. and worked for me. (I wanted Expand and Collapse events anyhow)

            .



            @Override
            public boolean onCreateOptionsMenu(Menu menu)
            {
            getMenuInflater().inflate(R.menu.menu_main, menu);
            MenuItem searchItem = menu.findItem(R.id.action_search);



            if(searchItem != null)
            {

            SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
            // use this method for search process
            searchView.setOnSearchClickListener(new View.OnClickListener()
            {
            @Override
            public void onClick(View v)
            {
            //Search view is expanded
            showSearchPage();
            }
            });
            searchView.setOnCloseListener(new SearchView.OnCloseListener()
            {
            @Override
            public boolean onClose()
            {
            //Search View is collapsed
            hideSearchPage();
            return false;
            }
            });
            searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
            {

            @Override
            public boolean onQueryTextSubmit(String query)
            {
            // use this method when query submitted
            Toast.makeText(MainActivity.this, query, Toast.LENGTH_SHORT).show();
            return false;
            }

            @Override
            public boolean onQueryTextChange(String newText)
            {
            // use this method for auto complete search process
            Log.e("SearchValueIs",":"+newText);
            return false;
            }
            });
            }
            return super.onCreateOptionsMenu(menu);
            }




            Hope It will help Someone...






            share|improve this answer
























              5












              5








              5






              After looking for a solution for couple of hours I've implement something like this. and worked for me. (I wanted Expand and Collapse events anyhow)

              .



              @Override
              public boolean onCreateOptionsMenu(Menu menu)
              {
              getMenuInflater().inflate(R.menu.menu_main, menu);
              MenuItem searchItem = menu.findItem(R.id.action_search);



              if(searchItem != null)
              {

              SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
              // use this method for search process
              searchView.setOnSearchClickListener(new View.OnClickListener()
              {
              @Override
              public void onClick(View v)
              {
              //Search view is expanded
              showSearchPage();
              }
              });
              searchView.setOnCloseListener(new SearchView.OnCloseListener()
              {
              @Override
              public boolean onClose()
              {
              //Search View is collapsed
              hideSearchPage();
              return false;
              }
              });
              searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
              {

              @Override
              public boolean onQueryTextSubmit(String query)
              {
              // use this method when query submitted
              Toast.makeText(MainActivity.this, query, Toast.LENGTH_SHORT).show();
              return false;
              }

              @Override
              public boolean onQueryTextChange(String newText)
              {
              // use this method for auto complete search process
              Log.e("SearchValueIs",":"+newText);
              return false;
              }
              });
              }
              return super.onCreateOptionsMenu(menu);
              }




              Hope It will help Someone...






              share|improve this answer












              After looking for a solution for couple of hours I've implement something like this. and worked for me. (I wanted Expand and Collapse events anyhow)

              .



              @Override
              public boolean onCreateOptionsMenu(Menu menu)
              {
              getMenuInflater().inflate(R.menu.menu_main, menu);
              MenuItem searchItem = menu.findItem(R.id.action_search);



              if(searchItem != null)
              {

              SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
              // use this method for search process
              searchView.setOnSearchClickListener(new View.OnClickListener()
              {
              @Override
              public void onClick(View v)
              {
              //Search view is expanded
              showSearchPage();
              }
              });
              searchView.setOnCloseListener(new SearchView.OnCloseListener()
              {
              @Override
              public boolean onClose()
              {
              //Search View is collapsed
              hideSearchPage();
              return false;
              }
              });
              searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
              {

              @Override
              public boolean onQueryTextSubmit(String query)
              {
              // use this method when query submitted
              Toast.makeText(MainActivity.this, query, Toast.LENGTH_SHORT).show();
              return false;
              }

              @Override
              public boolean onQueryTextChange(String newText)
              {
              // use this method for auto complete search process
              Log.e("SearchValueIs",":"+newText);
              return false;
              }
              });
              }
              return super.onCreateOptionsMenu(menu);
              }




              Hope It will help Someone...







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Sep 16 '15 at 6:56









              GreenRobo

              2,70611633




              2,70611633

























                  5














                  Android training



                  app:showAsAction="ifRoom|collapseActionView"





                  share|improve this answer


























                    5














                    Android training



                    app:showAsAction="ifRoom|collapseActionView"





                    share|improve this answer
























                      5












                      5








                      5






                      Android training



                      app:showAsAction="ifRoom|collapseActionView"





                      share|improve this answer












                      Android training



                      app:showAsAction="ifRoom|collapseActionView"






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jan 11 '16 at 3:03









                      txbnx

                      5111




                      5111























                          1














                          You can add ViewTreeObserver to track the visibility state of android.support.v7.appcompat.R.id.search_edit_frame. You can check my answer here: https://stackoverflow.com/a/28762632/1633609



                          You can see below how to handle Expand and Collapsed states!



                          To answer why MenuItemCompat.setOnActionExpandListener(...) is not working, this listener is only called if the showAsAction of the SearchView is set to MenuItemCompat.SHOW_AS_ACTION_ALWAYS (you can also add more options).



                          This is the copy of my answer form the other question:



                          I found that MenuItemCompat.setOnActionExpandListener(...) is not working if you don't pass:



                              searchItem
                          .setShowAsAction(MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
                          | MenuItemCompat.SHOW_AS_ACTION_ALWAYS);


                          But this is changing the SearchView and is replacing the DrawerToggle with back arrow.



                          I wanted to keep the original views and still track the Expanded/Collapsed state and use supported Search View.



                          Solution:



                          When android.support.v7.widget.SearchView is changing the view state the LinearLayout view's, with id android.support.v7.appcompat.R.id.search_edit_frame, visibility value is being changed from View.VISIBLE to View.GONE and opposite. So I add ViewTreeObserver to track the visibility change of the search edit frame.



                          menu_search.xml:



                          <menu xmlns:android="http://schemas.android.com/apk/res/android"
                          xmlns:app="http://schemas.android.com/apk/res-auto" >

                          <item
                          android:id="@+id/action_search"
                          android:icon="@android:drawable/ic_menu_search"
                          android:title="@string/search"
                          app:actionViewClass="android.support.v7.widget.SearchView"
                          app:showAsAction="always"/>

                          </menu>


                          In the activity:



                          import android.support.v4.view.MenuItemCompat;
                          import android.support.v7.widget.SearchView;
                          import android.view.Menu;
                          import android.view.MenuItem;

                          ..........

                          private View mSearchEditFrame;

                          @Override
                          public boolean onCreateOptionsMenu(Menu menu) {
                          getMenuInflater().inflate(R.menu.menu_search, menu);
                          MenuItem searchItem = (MenuItem) menu.findItem(R.id.action_search);

                          SearchView searchView = (SearchView) MenuItemCompat
                          .getActionView(searchItem);
                          searchView.setSubmitButtonEnabled(false);
                          mSearchEditFrame = searchView
                          .findViewById(android.support.v7.appcompat.R.id.search_edit_frame);

                          ViewTreeObserver vto = mSearchEditFrame.getViewTreeObserver();
                          vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                          int oldVisibility = -1;

                          @Override
                          public void onGlobalLayout() {

                          int currentVisibility = mSearchEditFrame.getVisibility();

                          if (currentVisibility != oldVisibility) {
                          if (currentVisibility == View.VISIBLE) {
                          Log.v(TAG, "EXPANDED");
                          } else {
                          Log.v(TAG, "COLLAPSED");
                          }

                          oldVisibility = currentVisibility;
                          }

                          }
                          });

                          return super.onCreateOptionsMenu(menu);
                          }





                          share|improve this answer




























                            1














                            You can add ViewTreeObserver to track the visibility state of android.support.v7.appcompat.R.id.search_edit_frame. You can check my answer here: https://stackoverflow.com/a/28762632/1633609



                            You can see below how to handle Expand and Collapsed states!



                            To answer why MenuItemCompat.setOnActionExpandListener(...) is not working, this listener is only called if the showAsAction of the SearchView is set to MenuItemCompat.SHOW_AS_ACTION_ALWAYS (you can also add more options).



                            This is the copy of my answer form the other question:



                            I found that MenuItemCompat.setOnActionExpandListener(...) is not working if you don't pass:



                                searchItem
                            .setShowAsAction(MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
                            | MenuItemCompat.SHOW_AS_ACTION_ALWAYS);


                            But this is changing the SearchView and is replacing the DrawerToggle with back arrow.



                            I wanted to keep the original views and still track the Expanded/Collapsed state and use supported Search View.



                            Solution:



                            When android.support.v7.widget.SearchView is changing the view state the LinearLayout view's, with id android.support.v7.appcompat.R.id.search_edit_frame, visibility value is being changed from View.VISIBLE to View.GONE and opposite. So I add ViewTreeObserver to track the visibility change of the search edit frame.



                            menu_search.xml:



                            <menu xmlns:android="http://schemas.android.com/apk/res/android"
                            xmlns:app="http://schemas.android.com/apk/res-auto" >

                            <item
                            android:id="@+id/action_search"
                            android:icon="@android:drawable/ic_menu_search"
                            android:title="@string/search"
                            app:actionViewClass="android.support.v7.widget.SearchView"
                            app:showAsAction="always"/>

                            </menu>


                            In the activity:



                            import android.support.v4.view.MenuItemCompat;
                            import android.support.v7.widget.SearchView;
                            import android.view.Menu;
                            import android.view.MenuItem;

                            ..........

                            private View mSearchEditFrame;

                            @Override
                            public boolean onCreateOptionsMenu(Menu menu) {
                            getMenuInflater().inflate(R.menu.menu_search, menu);
                            MenuItem searchItem = (MenuItem) menu.findItem(R.id.action_search);

                            SearchView searchView = (SearchView) MenuItemCompat
                            .getActionView(searchItem);
                            searchView.setSubmitButtonEnabled(false);
                            mSearchEditFrame = searchView
                            .findViewById(android.support.v7.appcompat.R.id.search_edit_frame);

                            ViewTreeObserver vto = mSearchEditFrame.getViewTreeObserver();
                            vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                            int oldVisibility = -1;

                            @Override
                            public void onGlobalLayout() {

                            int currentVisibility = mSearchEditFrame.getVisibility();

                            if (currentVisibility != oldVisibility) {
                            if (currentVisibility == View.VISIBLE) {
                            Log.v(TAG, "EXPANDED");
                            } else {
                            Log.v(TAG, "COLLAPSED");
                            }

                            oldVisibility = currentVisibility;
                            }

                            }
                            });

                            return super.onCreateOptionsMenu(menu);
                            }





                            share|improve this answer


























                              1












                              1








                              1






                              You can add ViewTreeObserver to track the visibility state of android.support.v7.appcompat.R.id.search_edit_frame. You can check my answer here: https://stackoverflow.com/a/28762632/1633609



                              You can see below how to handle Expand and Collapsed states!



                              To answer why MenuItemCompat.setOnActionExpandListener(...) is not working, this listener is only called if the showAsAction of the SearchView is set to MenuItemCompat.SHOW_AS_ACTION_ALWAYS (you can also add more options).



                              This is the copy of my answer form the other question:



                              I found that MenuItemCompat.setOnActionExpandListener(...) is not working if you don't pass:



                                  searchItem
                              .setShowAsAction(MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
                              | MenuItemCompat.SHOW_AS_ACTION_ALWAYS);


                              But this is changing the SearchView and is replacing the DrawerToggle with back arrow.



                              I wanted to keep the original views and still track the Expanded/Collapsed state and use supported Search View.



                              Solution:



                              When android.support.v7.widget.SearchView is changing the view state the LinearLayout view's, with id android.support.v7.appcompat.R.id.search_edit_frame, visibility value is being changed from View.VISIBLE to View.GONE and opposite. So I add ViewTreeObserver to track the visibility change of the search edit frame.



                              menu_search.xml:



                              <menu xmlns:android="http://schemas.android.com/apk/res/android"
                              xmlns:app="http://schemas.android.com/apk/res-auto" >

                              <item
                              android:id="@+id/action_search"
                              android:icon="@android:drawable/ic_menu_search"
                              android:title="@string/search"
                              app:actionViewClass="android.support.v7.widget.SearchView"
                              app:showAsAction="always"/>

                              </menu>


                              In the activity:



                              import android.support.v4.view.MenuItemCompat;
                              import android.support.v7.widget.SearchView;
                              import android.view.Menu;
                              import android.view.MenuItem;

                              ..........

                              private View mSearchEditFrame;

                              @Override
                              public boolean onCreateOptionsMenu(Menu menu) {
                              getMenuInflater().inflate(R.menu.menu_search, menu);
                              MenuItem searchItem = (MenuItem) menu.findItem(R.id.action_search);

                              SearchView searchView = (SearchView) MenuItemCompat
                              .getActionView(searchItem);
                              searchView.setSubmitButtonEnabled(false);
                              mSearchEditFrame = searchView
                              .findViewById(android.support.v7.appcompat.R.id.search_edit_frame);

                              ViewTreeObserver vto = mSearchEditFrame.getViewTreeObserver();
                              vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                              int oldVisibility = -1;

                              @Override
                              public void onGlobalLayout() {

                              int currentVisibility = mSearchEditFrame.getVisibility();

                              if (currentVisibility != oldVisibility) {
                              if (currentVisibility == View.VISIBLE) {
                              Log.v(TAG, "EXPANDED");
                              } else {
                              Log.v(TAG, "COLLAPSED");
                              }

                              oldVisibility = currentVisibility;
                              }

                              }
                              });

                              return super.onCreateOptionsMenu(menu);
                              }





                              share|improve this answer














                              You can add ViewTreeObserver to track the visibility state of android.support.v7.appcompat.R.id.search_edit_frame. You can check my answer here: https://stackoverflow.com/a/28762632/1633609



                              You can see below how to handle Expand and Collapsed states!



                              To answer why MenuItemCompat.setOnActionExpandListener(...) is not working, this listener is only called if the showAsAction of the SearchView is set to MenuItemCompat.SHOW_AS_ACTION_ALWAYS (you can also add more options).



                              This is the copy of my answer form the other question:



                              I found that MenuItemCompat.setOnActionExpandListener(...) is not working if you don't pass:



                                  searchItem
                              .setShowAsAction(MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
                              | MenuItemCompat.SHOW_AS_ACTION_ALWAYS);


                              But this is changing the SearchView and is replacing the DrawerToggle with back arrow.



                              I wanted to keep the original views and still track the Expanded/Collapsed state and use supported Search View.



                              Solution:



                              When android.support.v7.widget.SearchView is changing the view state the LinearLayout view's, with id android.support.v7.appcompat.R.id.search_edit_frame, visibility value is being changed from View.VISIBLE to View.GONE and opposite. So I add ViewTreeObserver to track the visibility change of the search edit frame.



                              menu_search.xml:



                              <menu xmlns:android="http://schemas.android.com/apk/res/android"
                              xmlns:app="http://schemas.android.com/apk/res-auto" >

                              <item
                              android:id="@+id/action_search"
                              android:icon="@android:drawable/ic_menu_search"
                              android:title="@string/search"
                              app:actionViewClass="android.support.v7.widget.SearchView"
                              app:showAsAction="always"/>

                              </menu>


                              In the activity:



                              import android.support.v4.view.MenuItemCompat;
                              import android.support.v7.widget.SearchView;
                              import android.view.Menu;
                              import android.view.MenuItem;

                              ..........

                              private View mSearchEditFrame;

                              @Override
                              public boolean onCreateOptionsMenu(Menu menu) {
                              getMenuInflater().inflate(R.menu.menu_search, menu);
                              MenuItem searchItem = (MenuItem) menu.findItem(R.id.action_search);

                              SearchView searchView = (SearchView) MenuItemCompat
                              .getActionView(searchItem);
                              searchView.setSubmitButtonEnabled(false);
                              mSearchEditFrame = searchView
                              .findViewById(android.support.v7.appcompat.R.id.search_edit_frame);

                              ViewTreeObserver vto = mSearchEditFrame.getViewTreeObserver();
                              vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                              int oldVisibility = -1;

                              @Override
                              public void onGlobalLayout() {

                              int currentVisibility = mSearchEditFrame.getVisibility();

                              if (currentVisibility != oldVisibility) {
                              if (currentVisibility == View.VISIBLE) {
                              Log.v(TAG, "EXPANDED");
                              } else {
                              Log.v(TAG, "COLLAPSED");
                              }

                              oldVisibility = currentVisibility;
                              }

                              }
                              });

                              return super.onCreateOptionsMenu(menu);
                              }






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited May 23 '17 at 11:53









                              Community

                              11




                              11










                              answered Feb 27 '15 at 10:38









                              Sir NIkolay Cesar The First

                              766719




                              766719























                                  0














                                  I know I'm very late to post this answer but hope it helps someone else. I recently came across the issue and I just made the Override methods return true and it worked like a charm.



                                          @Override
                                  public boolean onMenuItemActionExpand(final MenuItem arg0) {

                                  android.util.Log.d("AppLog", "onMenuItemActionExpand");
                                  return true;
                                  }

                                  @Override
                                  public boolean onMenuItemActionCollapse(final MenuItem arg0) {

                                  android.util.Log.d("AppLog", "onMenuItemActionCollapse");
                                  return true;
                                  }


                                  In more recent versions



                                        menuSearch.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
                                  @Override
                                  public boolean onMenuItemActionExpand(MenuItem item) {

                                  //SearchView appers
                                  return true;
                                  }

                                  @Override
                                  public boolean onMenuItemActionCollapse(MenuItem item) {

                                  //SearchView disappears
                                  return true;
                                  }
                                  });





                                  share|improve this answer


























                                    0














                                    I know I'm very late to post this answer but hope it helps someone else. I recently came across the issue and I just made the Override methods return true and it worked like a charm.



                                            @Override
                                    public boolean onMenuItemActionExpand(final MenuItem arg0) {

                                    android.util.Log.d("AppLog", "onMenuItemActionExpand");
                                    return true;
                                    }

                                    @Override
                                    public boolean onMenuItemActionCollapse(final MenuItem arg0) {

                                    android.util.Log.d("AppLog", "onMenuItemActionCollapse");
                                    return true;
                                    }


                                    In more recent versions



                                          menuSearch.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
                                    @Override
                                    public boolean onMenuItemActionExpand(MenuItem item) {

                                    //SearchView appers
                                    return true;
                                    }

                                    @Override
                                    public boolean onMenuItemActionCollapse(MenuItem item) {

                                    //SearchView disappears
                                    return true;
                                    }
                                    });





                                    share|improve this answer
























                                      0












                                      0








                                      0






                                      I know I'm very late to post this answer but hope it helps someone else. I recently came across the issue and I just made the Override methods return true and it worked like a charm.



                                              @Override
                                      public boolean onMenuItemActionExpand(final MenuItem arg0) {

                                      android.util.Log.d("AppLog", "onMenuItemActionExpand");
                                      return true;
                                      }

                                      @Override
                                      public boolean onMenuItemActionCollapse(final MenuItem arg0) {

                                      android.util.Log.d("AppLog", "onMenuItemActionCollapse");
                                      return true;
                                      }


                                      In more recent versions



                                            menuSearch.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
                                      @Override
                                      public boolean onMenuItemActionExpand(MenuItem item) {

                                      //SearchView appers
                                      return true;
                                      }

                                      @Override
                                      public boolean onMenuItemActionCollapse(MenuItem item) {

                                      //SearchView disappears
                                      return true;
                                      }
                                      });





                                      share|improve this answer












                                      I know I'm very late to post this answer but hope it helps someone else. I recently came across the issue and I just made the Override methods return true and it worked like a charm.



                                              @Override
                                      public boolean onMenuItemActionExpand(final MenuItem arg0) {

                                      android.util.Log.d("AppLog", "onMenuItemActionExpand");
                                      return true;
                                      }

                                      @Override
                                      public boolean onMenuItemActionCollapse(final MenuItem arg0) {

                                      android.util.Log.d("AppLog", "onMenuItemActionCollapse");
                                      return true;
                                      }


                                      In more recent versions



                                            menuSearch.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
                                      @Override
                                      public boolean onMenuItemActionExpand(MenuItem item) {

                                      //SearchView appers
                                      return true;
                                      }

                                      @Override
                                      public boolean onMenuItemActionCollapse(MenuItem item) {

                                      //SearchView disappears
                                      return true;
                                      }
                                      });






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Dec 27 at 14:24









                                      Blazing

                                      64




                                      64






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.





                                          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                          Please pay close attention to the following guidance:


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f27469236%2fmenuitemcompat-setonactionexpandlistener-doesnt-do-anything%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          Monofisismo

                                          Angular Downloading a file using contenturl with Basic Authentication

                                          Olmecas