how to hide the ChromeCast MediaRoute MenuItem in the ActionBar? - android

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.

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.

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.

Related

Android: Hide 3 dots from Navigation view header at right

How to hide 3 dots from Navigation header which comes in the right of header? This could be repeated question. I found few similar questions and their answers but they were for older version of Android. I am using Android sdk 21.
Any idea how to hide that 3 dot button?
Just Remove Override Method like this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_and_add, menu);
return true;
}
This Override Method is responsible to for creating three dote as you mention it's really OptionMenu. If you don't want it, don't override onCreateOptionsMenumethod.
Alternative
Don't Inflate the menu xml. Just block the line like this
//getMenuInflater().inflate(R.menu.search_and_add, menu);
other code can remain same. No problem at all..
Those "3 Dots" are the "Overflow" menu, and is created when you establish a menu using a file in the menu resources directory.
If you have buttons or functionality you are wanting to expose via you action bar, you will need to have the overflow buttons (or instead, you can choose to have your buttons exposed at the top level inside the Action bar.
If you really don't want a menu, get rid of the menu.xml file describing this menu, and then get rid of the onCreateOptionsMenu() from your Activity.
Here are the official docs, which describe how this works.
I think you are speaking about the options menu, to get rid of it remove the override of the method onCreateOptionsMenu
In your menu folder the xmlfile that is used by your activity, change the app:showAsAction="never" to app:showAsAction="always" or some other you can see the options that are availabe by pressing ctrl+space.
Or else to get rid of it completely just remove the whole code and it's corresponding usages.

Toolbar Won't Show Menu Button

I tried different methods of making a menu button on the Android Studio update but I couldn't make the menu button appear.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_mainmenu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
I have added this code in the Activity and made a menu.xml file containing:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<item
android:id="#+id/expanded_menu"
android:showAsAction="always"
android:icon="#drawable/ic_action_menu"
app:showAsAction="always"
android:title="button">
</item>
<item android:id="#+id/credits"
android:title="Credits"
android:orderInCategory="100"
/>
<item android:title="Settings"
android:id="#+id/settings"
android:orderInCategory="101"
/>
</menu>
Anyway, how I can make the three squares that were automatically added in the older versions of Android appear?
This is a screenshot of the application, in previous versions of android studio there were three dots at the top right corner.
Currently they are not there and my question is how do I add the menu button.
Edit: also while writing app:showAsAction="always" android studio marked all of the line red untill I wrote the "=" sign, maybe this error is related to something?
Also maybe I missed a step while making the items/toolbar, but I do not know exactly if I missed anything.
I believe you are talking about the overflow button (labeled '3'):
This button appears when there is not enough room to display all of the items in your options menu, or when you designate specific items to not be shown as an action. This is handled by the app:showAsAction attribute. There are five different values you can apply, which are described as follows:
ifRoom : Only place this item in the app bar if there is room for it. If there is not room for all the items marked "ifRoom", the
items with the lowest orderInCategory values are displayed as
actions, and the remaining items are displayed in the overflow menu.
withText : Also include the title text (defined by android:title) with the action item. You can include this value
along with one of the others as a flag set, by separating them with a
pipe |.
never : Never place this item in the app bar. Instead, list the item in the app bar's overflow menu.
always : Always place this item in the app bar. Avoid using this unless it's critical that the item always appear in the action
bar. Setting multiple items to always appear as action items can
result in them overlapping with other UI in the app bar.
collapseActionView : The action view associated with this action item (as declared by android:actionLayout or
android:actionViewClass) is collapsible. Introduced in API Level 14.
Therefore, if you want to only show the overflow button, you should set app:showAsAction="never" for all of your menu items.
Though, the typical structure for an options menu is to always show the first item, and designate the rest to be shown "ifRoom".

Is it possible to have a action bar menu using the appcompat library?

recently I've switched from the regular action bar implementation to the recently released appcompat implementation. My app made heavy use of the action bar to provide functionality. Since switching, on older spots
APIs (less than 11) don't have any menu items. And newer APIs do, but they don't show the image like configured (if room|withText). Has anyone else experienced this or came up with any solutions?
I found out what was up, when using the appcompat library. You can create your menu just like normal.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
But, in your menu xml files, add a xmlns:app attribute to the menu tag, like so:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
then, in each of your menu items, where you usually specify the "showAs" style (ifRoom, withText, etc.), include this alternative line alongside the regular one:
app:showAsAction="ifRoom|withText"
android:showAsAction="ifRoom|withText"
After this, your menus will show correctly on both current and old APIs. I got this information from here.
If there is a physical "Menu" button on device, it will show the contextual menu. If not, the menu item will be added to the ActionBar.

Popup menu : items reachable from the actionbar instead of the "menu button" of the device

I have in my application a ListView activity called ResultListViewActivity and which display contents from a database. The content displayed in the listview depends on a button clicked in a previous activity. So if the user clicks the "Button 1" in the Main Activity, it will display a specific content in ResultListViewActivity, and so on.
I want to add a menu to this activity : the thing is, the items of this menu are also supposed to change according to the button clicked before. There are 9 cases, each cases has a different number of items. So far I have this code :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
Bundle bundle = getIntent().getExtras();
int filterVariable = bundle.getInt("filterVariable");
switch (filterVariable)
{
case 1:
getSupportMenuInflater().inflate(R.menu.filter_fastfood, menu);
break;
case 2:
getSupportMenuInflater().inflate(R.menu.filter_restaurants, menu);
break;
case 3:
getSupportMenuInflater().inflate(R.menu.filter_coffeeshops, menu);
break;
case 4:
getSupportMenuInflater().inflate(R.menu.filter_bars, menu);
break;
// [... and so on]
}
return super.onCreateOptionsMenu(menu);
}
It works well, for every case I indeed have my different items in the menu. But I don't want these items to be reachable through the "menu button" of my device (which is absolutely not instinctive and hard for the user to guess). These items are importants so I would like to have a button on the upper right corner, which would open this menu instead (drop down menu / pop up menu).
Do you know how such a thing can be done ? I have seen this in some google application but couldn't find tutorial showing which tools to use for doing this.
Thanks !
ps: I am using actionbarsherlock in my application.
If you are using the latest version 4.2.0 of ABS, then support for .ForceOverflow themes has been removed.
Source: Version 4.2.0 Changelog
Extract of the Change Log:
Add SearchView widget for standard search interaction (API 8+ only)
Fix: ShareActionProvider in the split action bar no longer fills the entire screen.
Fix: ShareActionProvider now does file I/O on a background thread.
Fix: Automatically correct ColorDrawable not respecting bounds when used as a stacked background.
Fix: Ensure fragments collection is present before dispatching events.
Fix: XML-defined onClick searches the correct context for the declared method.
Fix: Ensure action mode start/finish callbacks are invoked on the activity for the native action bar.
Fix: Allow tab callbacks to have a fragment transaction instance for any FragmentActivity.
Fix: Ensure CollapsibleActionView callbacks are dispatched in both native and compatbility action bars.
Fix: Remove .ForceOverflow themes. These never should have been included.
To workaround that, you will need to download an older version (version 4.1.0) of ABS that had it working. You can get a list of download as per their release history here: http://actionbarsherlock.com/download.html
This is from a production app, tested and functioning.
You will also need to make a minor change in your applications default theme. For example:
<style name="MyTheme" parent="#style/Theme.Sherlock.ForceOverflow">
<item name="android:windowBackground">#color/background</item>
<item name="actionBarStyle">#style/Widget.Styled.ActionBar</item>
<item name="android:actionBarStyle">#style/Widget.Styled.ActionBar</item>
</style>
<style name="MyTheme.ForceOverflow">
<item name="absForceOverflow">true</item>
</style>
Note the use of #style/Theme.Sherlock.ForceOverflow in the parent attribute for the primary theme. Also not the additional <style> with the name of MyTheme.ForceOverflow. Doing the above will get the Overflow menu working for you.
You will also need to add this attribute to your menu items:
android:showAsAction="ifRoom|withText"
Now, to force the device into thinking that it has a physical menu key, use this code:
NOTE: This is a hack and for the obvious lack of access to multiple devices, I have personally never tested if it works on every Android device that exists. Use at your own risk etc. FYI, I have used this before and it worked with no complaints till date.
try {
ViewConfiguration config = ViewConfiguration.get(MainPage.this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if (menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
e.printStackTrace();
}
Forcing the Overflow Menu to appear in the ActionBar as against not showing an indicator that more options exist is a personal preference. I am answering this from the stand point of a technically possible solution as against promoting the use of such a feature. Whether it is advisable from the UX point of view is another subject matter.

action bar - positioning of action items

I'm having a go at doing an android 4+ layout for an application that remotely controls another device via wifi/bluetooth. And I'm trying to use the action bar as best it can be used.
In this app's case, I want to have a picker that selects basic system mode of operation for the remote device. And to the right of it I want an "edit setup" button. so user can either just select a mode of operation, or edit the selected mode of operation. I want the edit button right next to the picker because it makes sense to group them. These two actions surface most of the features people will normally use in the app.
I'm using the default example code for an android activity, as inserted in my project by the wizard "new>other>android activity" it has a picker already setup and working.
The issue is, that the picker gets put to the far left of the action bar, and all other action items start from the right and move to the left. I can't seem to lock the position of the "edit" action item immediately to the right of the inserted picker.
items in menu XML look like this:
<item android:id="#+id/edit_configuration"
android:title="Edit"
android:orderInCategory="2"
android:layout_gravity="left"
android:showAsAction="always" />
<item android:id="#+id/menu_settings"
android:title="#string/menu_settings"
android:orderInCategory="100"
android:showAsAction="never" />
The picker needs to be inserted using code in onCreate for the activity class.
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
// Set up the dropdown list navigation in the action bar.
actionBar.setListNavigationCallbacks(
// Specify a SpinnerAdapter to populate the dropdown list.
new ArrayAdapter(
actionBar.getThemedContext(),
android.R.layout.simple_list_item_1,
android.R.id.text1,
new String[]{
getString(R.string.title_section1),
getString(R.string.title_section2),
getString(R.string.title_section3),
}),
this);
I've tried the standard layoutGravity attribute in the menu XML, but while it causes no error, it also doesn't do anything I can see.
If anyone knows how I can attach my edit button to the immediate right of my picker, I'd be really pleased to hear about how it can be done.
The system controls the location of the action items to provide a common look and feel across all applications; in other words, there is no way to achieve this (unless someone else knows otherwise). You could, perhaps, extend the Action Bar, or in some way rewrite it to allow for the repositioning of action items, but that is beyond my skill.
Inflate from two menu xml files in oncreateoptionsmenu. File inflated at last will be at right most position.
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_one, menu);
inflater.inflate(R.menu.menu_two, menu);
Items of menu_two will be on right most side.

Categories

Resources