I am backporting my app to API7 with AppCompat and have a problem with the actionbar.
When I use FragmentActivity the actionbar is shown on my phone (API18), but with ActionBarActivity it shows up as the optionmenu by pressing the menubutton.
On the emulator with API7 the actionbar is always shown as an optionsmenu.
Any ideas?
Use the compat name space for your menu items like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:compat="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_whatever"
android:icon="#drawable/ic_action_whatever"
android:title="#string/whatever"
compat:showAsAction="ifRoom" />
</menu>
Related to a duplicate that points to this post, I was having trouble making my buttons appear as action items instead of overflow items, despite having showAsAction set to always. I managed to coerce it by extending my activity with Activity instead of ActionBarActivity. According to this answer, this is acceptable if you don't need to support api levels below 11.
...extends ActionBarActivity:
...extends Activity:
I do debug with Doogee Valencia Y100Pro, and menu as "three little square" not visible, but when I extended my MainActivity with android.support.v7.app.ActionBarActivity, then I obtain text/icon menu in action bar. Next screenshot and menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_update"
android:icon="#drawable/ic_refresh"
android:title="#string/action_update"
app:showAsAction="always"/>
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
app:showAsAction="ifRoom"/>
</menu>
Related
I'm using AndroidX Navigation architecture and want to know is it possible to replace the ActionBar menu completely or not?
In the project, in the Menu directory, there is a home.xml file that contains a menu items:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/homeMainMenu">
<item
android:id="#+id/action_inbox"
android:icon="#drawable/ic_baseline_mail_outline_24"
android:title="#string/action_inbox"
app:showAsAction="ifRoom" />
<item android:icon="#drawable/ic_baseline_more_vert_24"
android:title="#string/app_name"
app:showAsAction="always">
<menu>
<item
android:id="#+id/action_exitapp"
android:icon="#drawable/ic_baseline_exit_to_app_24"
android:title="#string/action_exit"
app:showAsAction="never" />
</menu>
</item>
</menu>
I want to create another menu XML file like mymenu.xml and on the fragment replace it with the home menu. Because of some reason don't want to replace items.
Is it possible? If "Yes" how to handle item click?
If i got your question correct then i guess you want to create different menu for activity and its fragment. You can do that. Please check here
Remember you've to add hasOptionsMenu in fragment onCreate
Icon of about in actionbar doesn't appear in the MainActivity it appear in the overflow menu but in the others Activity it appears without any problems
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" >
<item
android:id="#+id/about_app"
android:icon="#drawable/ic_action_about"
android:showAsAction="ifRoom"
android:title="#string/about_app"/>
</menu>
But when I change it to this
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" >
<item
android:id="#+id/about_app"
android:icon="#drawable/ic_action_about"
app:showAsAction="ifRoom"
android:title="#string/about_app"/>
</menu>
The icon of about appear in just the MainActivity and in the others Activitys it diseapper and it appear in the overflow menu
If you are using the Android Support library you should use app:showAsAction="ifRoom" (Note the app: namespace).
To make an action bar item visible always change ifRoom to always, otherwise, if the title is long the system will place the item in the overflow menu.
The problem come from the MainActivity because I extends ActionBarActivity for App who have API 11, to resolve the problem you should extends Activity and use the android:showAsAction and for API 11 or higher than 11.
A common pattern I see in many apps that provide some search functionality is to have the search widget clear the other menu icons off of the ActionBar. This works for me right now just by using ifRoom for showAsAction in my menu items. I'd like to show more menu items in the actionbar though, because it seems that the ActionBar is very conservative with how it determines ifRoom and consequently at least one important menu item is pushed to the overflow (perhaps someone smarter than me knows why they do that).
So how can I set showAsAction as always while still clearing the menu items off of the ActionBar when the Search Widget is activated? When I try that now, the menu items don't move, and the search widget is condensed.
Here's my menu:
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="collapseActionView|always"
android:actionViewClass="android.widget.SearchView" />
<item
android:id="#+id/action_stores"
android:title="#string/action_stores"
android:icon="#drawable/ic_action_stores"
android:showAsAction="always" />
<item
android:id="#+id/action_shopping_lists"
android:title="#string/action_shopping_lists"
android:icon="#drawable/ic_action_lists"
android:showAsAction="always" />
<item
android:id="#+id/action_preferences"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
My searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/search_hint" >
</searchable>
Couldn't you set an onClickListener on the search widget that then runs some few lines of code to make the other MenuItems change from 'always' to 'ifRoom'?
The few lines of code within the onClickListener would contain something like this:
myMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
whereas myMenuItem is your MenuItem that should go from 'always' to 'ifRoom'.
You can change it back later in an appropriate method.
I have two menu items: Settings and Exit. When I run my code, with both set to showAsAction:"always", none of them show up on the screen when I press the menu button on my phone. This also happens if I set both to "ifRoom". However, if I set one to "never", the other one will show up. How can I get both items to show up? I am running a ~3.5 inch android 4.1.2 phone. I am using the android:theme="#style/Theme.AppCompat" theme.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity" >
<group android:checkableBehavior="single">
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="ifRoom|withText"
android:icon="#drawable/sun"/>
<item android:id="#+id/exit_the_app"
android:title="#string/options_exit_text"
app:showAsAction="ifRoom|withText"
android:orderInCategory="101"
android:icon="#drawable/night"/>
</group>
</menu>
This is the screen when both items are set to "always" or "ifRoom", no menu items show up:
This is the screen when settings is set to "never", the exit item shows up:
There seems to be enough room, so how do I make both items show up? Thank you.
When showAsAction is set to ifRoom or always the item is shown in the action bar instead of the overflow menu (only if there's room in the case of ifRoom). This is the expected behavior.
If you want them to both show up in the overflow menu, set showAsAction to never for both.
I suggest reading the documentation: http://developer.android.com/guide/topics/ui/actionbar.html
I have an app (for 4.0 and above) which has couple of MenuItems in the ActionBar (one with 'always' and the rest with 'ifRoom' or 'never' property).
So the issue goes like this...
If I launch the app in landscape mode, the ActionBar looks like the following:
When I rotate to landscape from portrait, the ActionBar looks like this:
As you can see, more icons appear than they should be, i.e no space is left for title bar and the tabs menu (Navigation Menu). Actually, the 4 items are shown in the portrait mode (it's a split ActionBar) and it seems that the Android doesn't realise that there's no SplitActionBar there anymore.
Please suggest what I can do to fix this? :)
Edit
My menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_share"
android:icon="#drawable/ic_menu_share"
android:showAsAction="always"
android:title="#string/share"/>
<item
android:id="#+id/action_viewToggle"
android:icon="#drawable/ic_menu_view_as_grid"
android:showAsAction="ifRoom"
android:title="#string/switch_view"/>
<item
android:id="#+id/action_upload"
android:icon="#drawable/ic_menu_upload"
android:showAsAction="ifRoom"
android:title="#string/upload"/>
<item
android:id="#+id/action_newFolder"
android:icon="#drawable/ic_menu_new_folder"
android:showAsAction="ifRoom"
android:title="#string/create_folder"/>
<item
android:id="#+id/action_multiselect"
android:icon="#drawable/ic_menu_select_all"
android:showAsAction="ifRoom"
android:title="#string/selection_mode"/>
<item
android:id="#+id/action_refresh"
android:icon="#drawable/ic_menu_refresh"
android:showAsAction="ifRoom"
android:title="#string/refresh"/>
<item
android:id="#+id/action_settings"
android:icon="#drawable/ic_menu_settings"
android:showAsAction="never"
android:title="#string/action_settings"/>
</menu>
Make sure you take care of each menu item visibility in your menu.xml file, check out the xml ShowAsAction attribute.
here's an example:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/SOME_MENU_ITEM"
android:icon="#drawable/SOME_ICON"
android:showAsAction="ifRoom" | "never" | "withText" | "always" | "collapseActionView"
android:title="SOME_TITLE"/>
.
.
One of the important attribute xml for menu items is,
android:showAsAction ,that defines the visibility of the action item.
From your code it is evident that if there is room in action bar it'll display it.
So if you don't want it to appear specify android:showAsAction value as never so that whatever the orientation is it'll appear only in the overflow menu.