Why have android:actionLayout and android:icon at the same time? - android

I am reading about action bars in BusyCoder's Android guide. There is an example:
<item
android:id="#+id/add"
android:actionLayout="#layout/add"
android:icon="#android:drawable/ic_menu_add"
android:showAsAction="ifRoom"
android:title="#string/add"/>
What is the point to have both android:actionLayout and android:icon defined? What are the conditions when the icon would show up?
Thanks for helping out Android noob

What are the conditions when the icon would show up?
I can't rule out the possibility that the action bar will decide that there is not enough room for the actionLayout, but there is enough room for an action item, which would therefore show the icon. Also, with ActionBarSherlock, the icon is shown even in the overflow on Android 2.x devices, and so again if the actionLayout would be too large, the icon might get used. Also, if I used ifRoom|collapseActionView instead of just ifRoom, the icon would be used.
That being said:
My sample does not actually watch for this theoretical action item, should it appear, to do anything if the user clicks on it
I am guessing at possible action bar behavior, as I have not tried any experiments to force the action bar to decide whether there is enough room for all actionLayout-configured items

here android:icon is defined to supply an icon for your menu item. This is always optional but it is recommended to have an icon for the menu item.
android:actionLayout is defined to supply a custom view for the menu item again this is optional but if u need some Image Views, Text Views, Edit Text Views etc. in your menu item, this is the way to go.
If you need more help see these guides. These I think will be more helpful to you.
Action Bar Android Developer Guide
Menu Resource Android Developer Guide
Also take a look at this example. This may give you a much better idea about android Action bars.
vimaltuts.com/android-tutorial-for-beginners/android-action-bar-tab-menu-example
Thank You!

Related

Android - Action Bar Style

I want to show Action Bar like below.
I have already used this library clickhere
But it takes only two icons on Action Bar. If I add four items, then last two are shown me like menu. I want all in Action bar same as image above.
Is it possible? if yes, then HOW? Can I change Action Bar color or its default style available in Device?
Thanks in Advance.
I recommend you use this:
http://jgilfelt.github.com/android-actionbarstylegenerator/
to style your ActionBar.
If you want all the 4 icons to be there, just add this attribute to all the menu items:
android:showAsAction="always"
hello try this library
it sure help you And you may have to add you own logic to handle this in below 2.3 Android OS and above 3.0 Android OS.
there is many demo sample available like below :
There is no way you can be sure of the number of icons displayed in the action bar. There is a large varierty of Android devices out there and they all have different screen sizes. That's why you have to prioritize the actions available in a menu : the most important will be displayed as actions in the action bars and the others will be displayed in the "menu" : the action overflow.
You should think of actions in a functional way, not a graphical way.
With ref to Image that you attached you should decide which design pattern you want to use in your application, there are many UI patterns available for mobiles, tablets. The image you attached looks like Side Navigation UI pattern. It's better you decide which UI pattern perfect for your app.. then start implementing it with custom action bar libs (if you want action bar in less than Android 3.0 ver) or any other.

Is there something like a Quick Action for Holo-Themes?

For my application I need something like a Quick Action. There are plenty of useful libraries on github since this seems not to be a feature that is officially available via the Android SDK.
But I just stumbled across something in the official gmail app:
This (the dropdown menu) is exactly what I need for my application. But I'm wondering what the the best way to achieve this functionality is. An own Quick Action implementation? Some weird Button+Spinner meet up? Are there any libraries or design guidelines that may help me on creating such a widget?
The proper way is to use contextual action items (action modes). That will show action items in the action bars when you long-press an item. If you have more than fit on the action bar, they will be displayed in the overflow just as with regular action items.
http://developer.android.com/design/patterns/selection.html
That's the ActionBar overflow menu. The actions in there are normal MenuItem's configured to be organized on the overflow menu if there's not enough room to display them all
Here's the action bar documentation where you can find how to show/hide the MenuItem. Basically, you need to play around with the android:showAsAction xml attribute when defining your action bars.
android:showAsAction=["ifRoom" | "never" | "withText" | "always" | "collapseActionView"]
The widget used in the shown gmail application is a PopupMenu (introduced in API 11). It's behavior is documented and explained in detail here.

Is it possible to get the number of menu items displayed in the action bar?

I'm developing an app that uses ActionBarSherlock.
As you know, using android:showAsAction attribute, you can display an action menu on action bar. And, the number of menu items displayed on action bar differs according to user's environment(resolution, portrait/landscape and so on).
So, is it possible to get it by code?
No, you can't.
It varies based on a few factors and it would be safe if you just assume that it varies and can be anywhere from two to five (including a potential overflow menu item).
Did you try menu.size()? I'm not sure what you need exactly.
I was thinking doesn't this do the trick?: getSupportActionBar().getNavigationItemCount();
In my app i am using bottom navigation. I am accessing the menu items by accessing the size() property on menu.
for bottom navigation :-
bottomNavigation.menu.size()

Action Bar Checkable Icons?

I was wondering if it were at all possible to have checkable icons in an action bar in ICS? Not with a check, but with a android:stateChecked property to play around with. I want it so when the user clicks on the icon, they enter a certain mode (and the background of the icon would be a bright color to let them know they are still in that mode), and when they click it again they are taken out of that mode. I've played around with some selector xmls but nothing has worked...Any ideas?
EDIT: I just learned that Checkable items cannot appear in main menus, they can only be in submenus. However, is there a way to still get the behavior of a checkable? It's easy enough to have a boolean like isInModeA, but is there a way to programmatically change the background/icon of an Action bar item?
There is a way to programmatically change the background/icon.
You can use ActionBar.setBackgroundDrawable, for instance, or setCustomView, or setIcon (found on the same page).
You may consider using a dropdown-like ActionProvider to provide e.g. a radio-button-like "on/off" rather than a checkbox.
One approach you could try is to keep the state saved in some variable and just display different items based on this state. Obviously, some of these items can change the state. You might look at doing this by implementing a custom action provider.

Android Action Bar add all icon to overflow

I am updating my apps to have action bars, as Google seems to want developers to do now, and I am trying to get all the options of one app to all be in the overflow menu. I have searched far and wide but I can't seem to find anything that would make them do that. I'm pretty sure it would be something in the xml that defines the menu items. Do any of you guys know how to make all the menu options be in the overflow menu?
As CommonsWare said, showAsAction is the key.
Set android:showAsAction="never" on the if you are inflating an xml or use item.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); if you are creating the items with code.
You should consider using android:showAsAction="ifRoom" or item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); since the actionbar will contain a lot of empty space, if you don't fill it with something else that is.
They will all be in the overflow menu by default. You have to specifically do things to your menu resource XML files to change that, notably have android:showAsAction attributes on the <item> elements.

Categories

Resources