Domanda

I am trying to set up a search interface using the ActionBar in the context of a ChromeCast application (using code from CastCompanionLibrary and VideoBrowserActivity git projects). i need a way to hide the ChromeCast MediaRoute MenuItem (the ChromeCast button, for short). it is juxtaposed next to a search icon, and when the user clicks on the search icon, the ChromeCast button should disappear so as to allow the search view to expand (as much as possible the ActionBar).

first, the XML defining my ActionBar looks like the following.

<menu xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto" >
 ...
 <item android:id="@+id/media_route_menu_item"
  android:title="@string/media_route_menu_title"
  app:actionProviderClass="android.support.v7.app.MediaRouteActionProvider"
  app:showAsAction="always"/>
 <item android:id="@+id/action_search"
  android:title="@string/action_search"
  android:icon="@drawable/ic_action_search"
  android:actionViewClass="android.widget.SearchView"
  app:showAsAction="always"/>
</menu>

then, in my activity (sub-class of ActionBarActivity), i create the menu as follows.

public boolean onCreateOptionsMenu(Menu menu) {
 super.onCreateOptionsMenu(menu);
 getMenuInflator().inflate(R.menu.main, menu);
 MenuItem miSearch = menu.findItem(R.id.action_search); 
 SearchView view = (SearchView)miSearch.getActionView();
 SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
 view.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
 mediaRouteMenuItem = mCastManager.addMediaRouterButton(menu, R.id.media_route_menu_item);
 return true;
}

i tried to hide the MediaRoute menu item as follows (this approach was taken from another SO post).

public boolean onOptionsItemSelected(MenuItem item) {
 switch(item.getItemId()) {
  case R.id.action_search:
   mediaRouteMenuItem.setVisible(false);
   invalidateOptionsMenu();
   return true;
 }
}

however, when the user clicks on the search icon, the MediaRoute menu item is still visible.

  • it would also be nice to know (if it's possible to hide the ChromeCast button) how to make the button visible again if the user cancels the search operation.

any help is appreciated.

È stato utile?

Soluzione

I think that what you are seeing (or, now, saw) was dictated by the order of the action providers/action classes. An expanded collapsible action view will take over space to the end of the action bar, but not clobber things before it. Hence, putting SearchView first will let it take over the whole bar.

Altri suggerimenti

What I am saying here is just some thoughts that I have not tested, so take that into account when you read this. Since MediaRouterActionProvider is managed by the framework, I don't think you can manually hide it; I imagine system will override that upon its periodic scan. I see two options here:

  • Use MediaRouteButton instead of ActionProvider; that is also supported by CCL and the visibility of that is completely up to you (CCL provides some callbacks that can tell you when there is a device matching your filters or not and you can clearly do as you see fit with that data). This is my recommended approach

  • Use a hack: have two MediaRouteSelector; one that is associated with an appId that doesn't exist (or is not published and no device is associated with that) and a second one which is associated with your good appId. If you use the first selector, the button will disappear since there is no device qualified for that appId and second one wold behave normally.

Again, I think the first option is cleaner. Let us know if it works out for you or not.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top