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".
Related
In an Android app, we have an option menu item called "Contact", which we'd prefer to be shown as an action in the action bar (instead of hidden under the three-dot menu). So we use
menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
However when we do that, the "CONTACT" action takes away room from the activity's title. So the title gets cut off with ellipses:
Instead, we'd like the title string to take priority over "IF_ROOM" actions for space in the action bar. In other words, if there's not room to show both the title string and the "CONTACT" action, hide the "CONTACT" action under the three-dot option menu.
Is there a way to do this?
P.S. I realize there are other ways to save space in this action bar image, e.g. removing the "up arrow," shrinking some margin, etc. That may be helpful, but this question is not about other ways to save space.
in your menu add android:showAsAction="never"
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- menu -->
<item android:id="#+id/contact_id"
android:icon="#drawable/ic_action_contact"
android:title="#string/action_contact_id"
android:showAsAction="never" />
<!-- menu -->
</menu>
Or in code
MenuItem.SHOW_AS_ACTION_NEVER
I have added 5 items on toolbar and when I reviewed in medium and small phones then application name is not visible.
How can I set application name to show always and reduce toolbar's items size according to available devices?
If by toolbar you are referring to the app's ActionBar, then what you are looking for is an overflow menu (that three vertical dots option you see on the top right corner of apps). Reading Android's docs on menus might help.
Basically you want to make sure some, or all of your items have the android:showAsAction property set to never or ifRoom as follows:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action1"
android:orderInCategory="1"
android:showAsAction="ifRoom"
android:title="#string/action1_string"/>
<item
android:id="#+id/action2"
android:orderInCategory="2"
android:showAsAction="never"
android:title="#string/action2_string"/>
</menu>
The key here is the android:showAsAction property, which allows you to specify when should an item be added to the bar and when it should be added to the overflow menu. Possible values are:
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 |. E.g. : android:showAsAction="ifRoom|withText"
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.
never: Never place this item in the app bar. Instead, list the item in the app bar's overflow menu.
collapseActionView: The action view associated with this action item is collapsible. API 14 onwards.
Source.
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.
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.
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.