Android Actionbar compatibility alternate xml for pre-honeycomb - android

I am using the actiobarcompat sample in my application and I am trying to implement search for pre 3.0 devices.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_search"
android:orderInCategory="1"
android:title="#string/menu_search"
android:icon="#drawable/ic_home"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
</menu>
On Honeycomb+ this works fine, the searchview widget appears in the actionbar. What I am trying to do is have a second menu XML so I can fall back to the old search activity way of doing it. However, there is no such thing as menu-v11 folder as the menu folder is essentially menu-v11 because that is the version it started supporting this.
My question is, using the actionbar compatibility sample, is there a way to declaratively add an alternate button for pre-honeycomb?

Can you please be more specific about what you are trying to achieve?
It it's about calling different activities depending on API version, the action bar has nothing to do with that.
You analyse API version in onOptionsItemSelected and act accordingly.
If you want different menu items depending on API version, just create a folder menu-v11 (or menu-v14) and put
version-specific xml-s- there.
BTW, I use com.android.actionbarcompat and it works great for me!

Related

Arrow missing from share icon within action bar

After creating a menu for my action bar. The little arrow doesn't appear next to the share icon and I'm not sure why this has happened or if it's intentional based on my code.
<?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_info"
android:title="#string/information"
android:icon="#android:drawable/ic_menu_info_details"
app:showAsAction="ifRoom"/>
<item android:id="#+id/action_share"
android:title="#string/menuitem_share"
android:icon="#android:drawable/ic_menu_share"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
app:showAsAction="ifRoom"/>
<item android:id="#+id/action_preferences"
android:title="#string/menuitem_preferences"
app:showAsAction="never"/>
</menu>
After viewing images of similar menus online, I noticed that little arrow (in this screenshot).
How can that little arrow be added to the share icon?
As far as I know, every new project you create in Android Studio uses the Material design (introduced with Android 5.0 Lollipop) with AppCompat themes. It is designed to show up on every compatible device thanks to Support Library (even on pre-Lollipop devices), and ActionBar menu items created when you are using Support Library looks like on the first image.
As far as I also know, I've seen this kind of arrowed Share button on the second image last time on a 4.x device. So I think you could re-create this kind of Share button if you would not use any theme and let your pre-Lollipop device to do the work with the ActionBar.
I've tried to recreate this behaviour by your code above, without luck. But then I opened the ApiDemos app which is on every Android Studio emulator image and shows a lot of functions built up in Android, for example ActionBar functions. There I saw that Share button looks differently on different API levels.
Android 4.4 KitKat:
Android 5.0 Lollipop:

Text or Icon in action bar, stuck with the 3 dots

I'm making an android app in Eclipse. I want to place a text or icon in the action bar, but when I am writing in menu.xml, which looks like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/createnew"
android:orderInCategory="1"
android:showAsAction="ifRoom"
android:title="CREATE"/>
</menu>
it puts it in the options menu. I want to separate it.
I see this in the app when I run it:
(sorry can't post pictures because I don't have enough rep)
I want to CREATE next to the option menu like this for example:
Since you are using the AppCompat Actionbar, you need to use a custom namespaced showAsAction attribute.
It should look something like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/createnew"
android:orderInCategory="1"
yourapp:showAsAction="ifRoom"
android:title="CREATE"/>
</menu>
Note that this uses yourapp:showAsAction instead of android:showAsAction. This is because the showAsAction attribute is not available on pre-Honeycomb devices and is provided by the support library.
For more information, read the Action Bar developer guide.
It's caused by this line
android:showAsAction="ifRoom"
That tells Android to put it up there if it's going to fit nicely. If you want it up there no matter the fit use
android:showAsAction="always"

Android menu item showAsAction="always" doesn't work

I have tried setting the:
android:showAsAction=".."
to every one of these:
ifRoom, never, withText, always, collapseActionView
but I always got the same result, which is not having any buttons on the action bar, so I have to press the 'menu' button.
Here is a picture of the menu now :
<item android:id="#+id/smth1"
android:title="#string/smth1"
android:showAsAction="always"
android:orderInCategory="1" />
I have even tried adding this:
android:uiOptions="splitActionBarWhenNarrow"
into application manifest file, but with no positive result (nothing changed).
I have tried running it on various kind of APIs (14, 16, 17, 19), but with the same result.
If my question seems to be unclear, here is a picture of a menu, which I would like to have:
Thanks for any help.
You need to use the compatibility namespace (see here)
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/menu_add_size"
android:title="#string/menu_add_item"
android:icon="#android:drawable/ic_menu_add"
app:showAsAction="always"/>
</menu>
Once you're using that you can use as many menu buttons as will fit. You're no longer limited to just two buttons + overflow showing.
You maybe just don't have enough space, you should first remove the title from your Activity by adding this to your onCreate method:
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
Then you can see here that the maximum number of icons you can display is directly linked to the width of your screen, on small phones you will only be able to see 2 icons in the ActionBar.

SearchView in ActionBar using support-v7-appcompat

I've been trying very hard to get the SearchView widget to expand in the actionbar using the support-v7 libraries. I've managed to get it working without the support libraries when I target 4.0+ but I want to write the app for 2.3+ so I need to use the support libraries.
I created a blank new activity with the following menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
    
<item
android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_search"
yourapp:showAsAction="always"
yourapp:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
</menu>
This does not even show the search button, let alone expand it upon clicking. It simply add's the search into the menu instead of showing it in the actionbar.
Alternatively I tried the same without the appcompat library , I simply replaced the menu.xml with:
<item
android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_search"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView"
android:title="Search"/>
And it works perfectly fine, and even expands to the search text input widget upon clicking.
I want the searchview as available in the second picture while using the appcompat library, but for some reason it doesn't seem to be working. I'm using eclipse and I've included the Support libraries with resources exactly as specified in Support Library Setup[developer.android.com].
My manifest file has minsdk version as 7, targetsdk version as 18, and the build target is also 18.
I suspect something is amiss in the support library setup, can someone please tell me what I might be doing wrong? Thanks!
Maybe SearchView wasn't showed because you missed to add a collapseActionView in this line: yourapp:showAsAction="always".
Also, your activity must extends AppCompatActivity. So, add AppCompat library to project
More details you can read on this link
Hope it will help you.

inflate options list after clicking a button

I would like to inflate an option list like this one after clicking on a certain button.
I didn't like to call it option menu because I'm not pressing the "menu" button.
I want the items to be listed like the one above..
Is it possible to use the same .xml file used to create an option menu inflater for my case?
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/first_menu_item"
android:title="First Menu Item">
</item>
<item
android:id="#+id/second_menu_item"
android:title="Second Menu Item">
</item>
<item
android:id="#+id/third_menu_item"
android:title="Third Menu Item">
</item>
<item
android:id="#+id/fourth_menu_item"
android:title="Fourth Menu Item">
</item>
</menu>
I hope some experts can help me achieve what I really want to do.
That's a popup menu, which is documented in the API guide.
However, depending on what you're trying to do, it may not be consistent with Android design guidelines. I encourage you to follow the guidelines whenever possible, because this will make your app easier to use. As users see more and more UI consistency across apps, Android itself becomes easier to use.
You can use a ContextMenu.
Activities and Fragments have the registerForContextMenu() and onCreateContextMenu() to quickly implement one of these. If you ever want to open a ContextMenu with a regular OnClickListener (rather than waiting for a long click event) you can use openContextMenu() directly.
See the Developer's Guide on Menus for detailed information and example code.
And if you are using API 11+ you can use a PopupMenu instead, this is describeed in the Menu guide as well.

Categories

Resources