Can anybody please explain me about this? I couldn't find sufficient information in developer documentation.
Is it also applicable when item is shown as action and long clicked on icon shows a toast containing title.
In short: it shows up when space is nice to be kept.
I took pictures on Galaxy S4 4.4.2.
The first picture is a landscape, having only three selection related icons, you can see that the condensed title is used there:
however if I don't supply the the titleCondensed it uses title and it clearly fits:
The full title is used at any other places where space is not an issue, like the "tooltip" (long press on an action bar icon, internally called cheat sheet) or the overflow menu:
... and here's part of code I used (with appcompat-v7):
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- Possibly more items like "Edit Item" and "Delete Item" below -->
<item android:id="#+id/action_select_all"
android:icon="#drawable/ic_action_select_all"
android:title="Select All"
android:titleCondensed="All"
app:showAsAction="ifRoom|withText" />
<!-- Same for Select None <-> None -->
<!-- Same for Invert Selection <-> Invert -->
</menu>
All of the above is empirical observation, and hence probably incomplete, but I was curious, so here are the Android code points implementing the below:
Note the internal package and these are from the framework not the support library!
MenuItemImpl.getTitleForItemView returns the title based on prefersCondensedTitle, which has three implementations:
Action Bar icon and text: ActionMenuItemView: true
Action Bar overflow probably: ListMenuItemView: false
Old style Options Menu: IconMenuItemView: true
http://developer.android.com/guide/topics/resources/menu-resource.html
Simply put, the titleCondensed is the title you would like to show when the title string is too long for the device it's being showed on.
Related
I have this existing group, called group0, defined in xml:
<group android:id="#+id/group0"
android:checkableBehavior="single">
<item
android:id="#+id/nav_camera"
android:icon="#drawable/ic_menu_camera"
android:title="Import" />
<item
android:id="#+id/nav_gallery"
android:icon="#drawable/ic_menu_gallery"
android:title="Gallery" />
</group>
I want to add more options to it:
nav_view.menu.add(groupId = R.id.group0, itemId = 85621, order = 12345, title = "adding to group 0")
Despite of the targetGroup parameter, this does not add to group0. I can confirm this by removing everything in group0:
nav_view.menu.removeGroup(R.id.group0)
The new item is not removed. It also does not share the checkableBehavior defined in XML.
How can I programmatically add new items to an existing group?
After reading Android source code, I think I got an idea for the problems here.
The new item was indeed added to group0 as advertised. It can be verified through its groupId property. But it doesn't behave like one. This appears to be 2 separate issues I was having.
Item is not removed when the group is removed.
This turns out to be a combination of misconfiguration and android bug. In the source code of removeGroup(int groupId) method, android starts iterating each menu item and checks its groupId. But it aborts the process if the next item no longer belongs to the target group. This is an acceptable optimization assuming all items in the same group will show up next to each other in the navigation drawer. The "misconfiguration" here is that I passed in a large integer for order, making my item show up much later down the list.
The solution here is to pass 0 for order parameter when adding the menu item, so they show up next to existing items in the group. Alternatively, override removeGroup function and remove the "optimization" code.
Item does not share the checkableBehavior defined by the group in XML.
This is because isCheckable is actually a property for each menu item, instead of a group property. So when new items are added, they do not automatically have isCheckable set correctly (although technically the add method in Android could have easily done it for us).
The solution here is to call isCheckable = true on each of the newly added item.
I understand that this question has been asked here many times but after of spending hours searching for the solution I am unable to find one.
My requirement simple, I just want to change share icon on Action Bar to white color. I've the white drawables and using built-in Action Bar.
have tried setting icon in menu xml but didn't work. Any help would be appreciated.
<item
android:id="#+id/action_share"
android:actionProviderClass="android.widget.ShareActionProvider"
android:showAsAction="ifRoom"
android:icon="#drawable/ic_action_share_white"
android:title="#string/action_share"/>
Take a look at the ShareActionProvider's onCreateActionView() method:
public View onCreateActionView() {
// Create the view and set its data model.
...
// Lookup and set the expand action icon.
TypedValue outTypedValue = new TypedValue();
mContext.getTheme().resolveAttribute(R.attr.actionModeShareDrawable, outTypedValue, true);
Drawable drawable = mContext.getResources().getDrawable(outTypedValue.resourceId);
...
So, to change this image, it looks like you should change the value of actionModeShareDrawable for your app's theme. Unfortunately this attribute seems not to be public in the framework (though it is if using AppCompat or ActionBarSherlock).
If you are using neither of these libraries, a possible solution is to create a subclass of ShareActionProvider and reimplement the onCreateActionView() method (though you'll have to duplicate its code and, possibly, the resources it uses).
I declared my menu items with android:showAsAction="ifRoom". My Activity is extending ActionBarActivity. In preview it is as expected but when run on my phone is it always in overflow.
From what I've seen nothing else is needed. My phone runs in Android 4.4.2 KitKat.
Am I missing anything else?
You have to define your own namespace, if you want to use the showAsAction attribute with ActionBarCompat(I assume you are using ActionBarCompat because you mentioned, that you're extending ActionBarActivity):
xmlns:app="http://schemas.android.com/apk/res-auto"
and use showAsAction like this:
<item [...] app:showAsAction="always"></item>
Just try this...
<item android:id="#+id/action_blah"
android:icon="#drawable/ic_c"
android:title="#string/action_blah"
app:showAsAction="(yourOptions)"/>
(yourOptions):
always = To force to show on the actionbar.
never = Not to show on your actionbar ever.
and
ifRoom = It will let your item if there is enough space on your action bar.
You can see these in Landscape mode.
HappyCoding
I have just starting using an ActionBar, everything is well apart from the search box. I am now wanting to use a SearchView but have found it to be hard to customize, is there a way to use my existing EditText based layout in place of the expandable SearchView?
The top one is the new SearchView and the bottom is my old one which I would like to use, I am not worried about any extras like autocomplete.
I have looked on google for some time but cannot find any solution, my XML for the menu looks like so :
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/btnSearch"
android:title="Search"
android:icon="#drawable/ic_action_search"
android:showAsAction="always|collapseActionView"
android:actionViewClass="android.widget.SearchView"
android:queryHint="blah blah"/>
...
</menu>
I have found that I can get the bottom line to look similar to my old search box by using some ugly looking reflection code, however it still does not look right, the padding etc is all wrong. I don't want any reflection, I just want to use my old layout.
I found something that could solve your problem:
http://www.techrepublic.com/blog/software-engineer/pro-tip-customize-the-android-search-view-widget/#.
I wrote this class to help with SearchView customization:
https://gist.github.com/jaredrummler/c408c9d897fd92d5d116
You could do something like this after inflating the menu in onCreateOptionsMenu:
SearchViewStyle.on(menu, R.id.your_search_id)
.setSearchPlateDrawableId(R.drawable.your_edit_text);
Is there a way to position the title text for a menu item to the left of the icon rather than to the right in an ActionBar? So in:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/forward"
android:title="Begin"
android:icon="#drawable/ForwardArrow"
android:showAsAction="always"></item>
</menu>
It would appear "Begin ->" instead of "-> Begin"
While you can define things like android:actionButtonStyle in a theme and apply that to your activity, I am not aware that you can use that to turn around and affect the positioning of the image versus the text.
However, you can use android:actionLayout in your <item> element to replace the stock action button with something else of your own design. If you putter around the Android source and find where these action buttons come from, I suspect that you will find that they are styled Button objects using the compound drawable stuff to put an image alongside the text caption. Cloning that and switching the side for the image, then using that for your actionLayout, should work, at least in theory.