SearchView in ActionBar using support-v7-appcompat - android

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.

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:

ActionBar menuitems doesnt work using support library

my menu layout looks like this
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:blabla="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/msg_action_sections"
android:title="Sections view"
android:orderInCategory="100"
blabla:showAsAction="never" />
<item android:id="#+id/action_search"
android:title="Search"
blabla:showAsAction="collapseActionView"
blabla:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
but eclipse keeps giving me this error : "Should use android:showAsAction when not using the appcompat
library"
I don't know how to tell Eclipse that I'm using it!!! In android studio it worked, but I can't use Android studio. So what should I do?
Thank you
PS: Im using support.v7.app.ActionBar and my Activity extends from ActionBarActivity
Ok I figured it out, problem is, that I had bad theme using in AndroidManifest. You have to use Theme which uses Appcompat as parrent:)

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 Actionbar compatibility alternate xml for pre-honeycomb

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!

How to force overflow menu on android actionbar compat?

Android action bar compat
Is it possible? On older devices (pre 3.0) the items that don't fit the action bar are only shown when the menu key is pressed, I want these items to be grouped in the actionbar's overflow menu.
The action overflow menu is only available when there is no hard menu button available on the device. I found this stated in the Framework Topics under User Interface > Action Bar, check out the 3rd bullet here.
There is an action bar library written by Jake Wharton called ActionBarSherlock. Perhaps this is able to supply you with an action overflow menu style even when on older devices (which include a hard menu button), however I have not looked into this.
Edit: ActionBarSherlock 4.0 (currently a release candidate) has functionality built in to force action overflow. If you want to extend the ActionBarCompat example yourself, you could take a look on github to get an idea how Jake implemented it. I would suggest just looking into using his library all together, as it is very well done.
If you choose to use Jake's library, look into setting up the Activity theme as #style/Theme.Sherlock.ForceOverflow to force the overflow menu on older devices.
Edit2: Using ForceOverflow theme causes issues (example #1) on devices with hardware menu button. Thus, Jake Wharton is going to remove ForceOverflow in the future versions.
Okay, this is simple but hard to figure out.
You first need a menu item you want to use as the overflow inflater. Example
<item
android:id="#+id/a_More"
android:icon="#drawable/more"
android:showAsAction="always"
android:title="More">
</item>
Once you have your item, add a sub-menu containing your items you want in the overflow menu. Example:
<item
android:id="#+id/a_More"
android:icon="#drawable/more"
android:showAsAction="always"
android:title="More">
<menu>
<item
android:id="#+id/aM_Home"
android:icon="#drawable/home"
android:title="Home"/>
</menu>
</item>
On click this will inflate other items within. My application is using ActionBarSherlock 4.0 so before this will work for you, you will need to access the "SplitActionBar". (Will still work on default android Actionbar)
Here's how:
In your AndroidManifest.xml file, you need to add this code under the activity you need the overflow menu in.
Honestly it shouldn't matter if you have the actionbar split or not but I prefer it.
android:uiOptions="splitActionBarWhenNarrow"
NOTE: Your item that inflates your overflow menu MUST showAsAction="always"
Vwola! you have an overflow menu! Hope I helped you out. :)
Following LeviRockerSk8er's suggestion I've forced to have a overflow menu in the action bar like this:
This is the code for "menu.xml":
<item
android:id="#+id/web_clasica"
android:icon="#drawable/ic_action_web_site"
android:showAsAction="ifRoom"
android:title="#string/menu_web"
/>
<item
android:id="#+id/overflow_fijo"
android:icon="#drawable/ic_action_core_overflow"
android:showAsAction="always"
android:title="#string/menu_email"
>
<menu>
<item
android:id="#+id/email"
android:icon="#drawable/ic_action_new_email"
android:showAsAction="ifRoom"
android:title="#string/menu_email"
/>
<item
android:id="#+id/share"
android:icon="#drawable/ic_action_share"
android:showAsAction="ifRoom"
android:title="#string/menu_share"
/>
<item
android:id="#+id/about"
android:showAsAction="ifRoom"
android:icon="#drawable/ic_action_action_about"
android:title="#string/menu_about"/>
</menu>

Categories

Resources