action bar - positioning of action items - android

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.

Related

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".

WebViewAdd Custom Menu Item in Contextual Action Bar with overriding existing

I need to add some custom menu items in contextual action bar for WebView. My current research shows that only one of the following two things can be done.
Override action mode to supply custom action items. In this case default items are gone.
Use system provided action mode. In this way, I can't add my menu items.
May this will solve your problem.
https://github.com/TheFinestArtist/FinestWebView-Android

Application Name in toolbar hide when 5 item added in it , how to show app name always in toolbar?

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.

How to get notified when when action items get into the overflow menu?

When you put a lot of action items on the action bar, some of them get to be inserted into the overflow menu, or become available by clicking on the menu button (if present).
Is there a way to be notified when this occurs? Maybe even know which items got to be into the overflow menu (or available via the menu button) ?
Again, I don't ask how to put items there and I don't need to get explanation about how to define action items. I ask how to know when and which items get to be there.
You cannot decide which item to be into Action overflow menu but you can decide which to be shown always on Action bar.
You can also use "always" to declare that an item always appear as
an action button. However, you should not force an item to appear in
the action bar this way. Doing so can create layout problems on
devices with a narrow screen. It's best to instead use "ifRoom" to
request that an item appear in the action bar, but allow the system to
move it into the overflow when there's not enough room. However, it
might be necessary to use this value if the item includes an action
view that cannot be collapsed and must always be visible to provide
access to a critical feature.
http://developer.android.com/guide/topics/ui/actionbar.html

how to hide the ChromeCast MediaRoute MenuItem in the ActionBar?

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.

Categories

Resources