Search Widget doesn't open action view in ActionBar Android - android

I am trying to achieve a uniform action bar across my app. I am currently trying to add a search widget. My app displays the magnifying glass, another icon, and the overflow menu icon in its actionbar.
I haven't yet added any search functionality I am just trying to get the icon to display how I want it.
The problem I have is that on my launch page the magnifying glass is displayed, and if clicked the field for entering a search term appears.
If the user then goes to another activity the magnifying glass will still appear in the action bar but if clicked nothing will happen. The only difference is that other activities display the "Up" arrow to the left of the android logo in the action bar but I don't think this is the reason the search widget isn't working.
How can I fix this so the user can enter a search term from any page?
Below is my xml code:
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/search_title"
android:showAsAction="collapseActionView|always"
app:showAsAction="always"
android:actionViewClass="android.widget.SearchView" />
My onPrepareOptionsMenu(), onCreateOptionsMenu() and onOptionsItemSelected() are all identical in every activity and since they work for the main page I believe the problem lies within this xml file.
I would prefer if the search would display always rather than moving into the overflow menu.
Thanks for your time

Just found a solution, I am using the support library for compatibility so I needed to use:
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/search_title"
android:showAsAction="collapseActionView|always"
app:showAsAction="collapseActionView|always"
app:actionViewClass="android.widget.SearchView"
android:actionViewClass="android.widget.SearchView" />
(I needed to add app:actionViewClass). I used this explanation to help me
https://stackoverflow.com/a/22829368/3707803

Related

How to access or show popup from android Recents build-in button

I am trying to show the popup from android third button also called 'Recents' button one like whatsapp. Here is my screenshot which I want to achieve.
Edit
Popup must show from the right most button click.
Can anyone help me how can I access that button.
The menu on your picture is called 'overflow menu' and it shows action bar items that don't fit into the action bar (there is not enough space for them). It's shown at the bottom of the screen because your device has Menu button (on Samsung phones the most left button at the bottom of the screen).
Please note that the menu pops up from overflow button (three dots) in the top right corner if your device doesn't have Menu button. Refer Action bar official guide.
You can add items to overflow menu by specifying android:showAsAction="never" in you menu.xml file. It would look like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/item0"
android:icon="#drawable/icn_info"
android:orderInCategory="1"
android:showAsAction="always"
android:title="Action bar item"/>
<item
android:id="#+id/item1"
android:orderInCategory="10"
android:showAsAction="never"
android:title="Item 1 in overflow"/>
<item
android:id="#+id/item2"
android:orderInCategory="20"
android:showAsAction="never"
android:title="Item 2 in overflow"/>
<item
android:id="#+id/item3"
android:orderInCategory="30"
android:showAsAction="never"
android:title="Item 3 in overflow"/>
</menu>
EDIT:
As you can see, my device doesn't have Menu button at the bottom, so my WhatsApp shows overflow button in Action bar and overflow menu is coming from there:
EDIT 2:
Now I get your point. However, overriding Recents button is highly discouraged because it causes bad user experience. Recents button is never used for showing menu, it is used for showing Recent apps / documents.
Also, some devices don't even have this button (e.g. some Samsung devices use long press on Home button to show Recents).

action bar menu color changed by SearchView

I am using an menu in action bar with a SearchView in it , the xml defined as:
<item
android:id="#+id/search"
android:orderInCategory="1"
android:showAsAction="ifRoom"
android:icon="#drawable/search"
android:onClick="search"
android:actionViewClass="android.widget.SearchView"
android:title="search"
/>
but from the attached image(the first menu and the 3rd menu has the same image), the search image which should has the white color as the left icon has another background color similiar to light gray. Could any one tell me how to solve this?
I'm not sure what the solution is, but I can tell you why it is happening. The SearchView uses its own icon to display on the action bar. So, when you set the icon in the XML it gets overriden by the default SearchView icon. I'd be interested in anyone's answer to this because this is a problem I actually ran into myself without being able to find an answer.

Android Action Bar actions not showing all options, or in middle of UI, and other problems

I cannot get my action bar to work right at all. There's way too many crazy examples to run through, but I have encountered things like the action bar overflow bottom showing up halfway down the UI in 4.0.3 (I'm using android:uiOptions="splitActionBarWhenNarrow"), to clicking item A yet item B showing as the selecting item in OnOptionsItemSelected(MenuItem), or not showing them all.
Not trying to do anything crazy. I just want a refresh icon, info icon, share icon to show up and then 2 extra options to show up from clicking the dotted overflow icon. But I cannot get it to work consistently. I need to support 4.0+.
Here's what I have right now. I have slaughtered the menu xml while pulling my hair out, but maybe someone can shed some light on what I must be doing horribly wrong?
This current xml causes the bottom split action bar to sit right in the middle for the 4.0.3 AVD:
<item android:id="#+id/action_refresh"
android:icon="#drawable/ic_action_refresh"
android:title="#string/action_refresh"
android:showAsAction="always" />
<item android:id="#+id/action_info"
android:icon="#drawable/ic_action_about"
android:title="#string/action_info"
android:showAsAction="always" />
<item
android:id="#+id/menu_item_share"
android:showAsAction="ifRoom"
android:title="#string/action_share"
android:actionProviderClass="android.widget.ShareActionProvider" />
<item
android:id="#+id/action_category"
android:showAsAction="never"
android:title="#string/action_category" />
<item
android:id="#+id/action_about"
android:showAsAction="never"
android:title="#string/action_about"/>
Seems like the share action provider was cause of most the issues. Just using an icon with click handler that builds a share Intent instead.
Similar issue:

Android: Margin between ActionBar icon and Navigation dropdown menu changes when you click search

I have the following setup:
So you can click on the search icon for the action bar to turn into a search bar. Pretty standard:
So if you're done searching you can click the Home icon to make the bar go away:
But when you go back, the navigation dropdown menu is shifted to the right quite a bit:
I can't really figure out what is happening here. The first thing I did was make a brand new app and recreate it (which is what you see below) to make sure it wasn't an issue with styles or themes I'm using in my main app. That didn't help the problem. I'm using ActionBarSherlock, so the next thing I did was rebuild the project using Google's official Action Bar API. That didn't make a difference either.
Does anyone have any ideas? I'll provide any code you guys think could help, but I took the implementation pretty much straight from Google's docs about it. Thanks!
EDIT: Adding some before-after images with layout borders visible (dev options feature in 4.1+)
BEFORE:
AFTER:
I've also included the menu XML:
<?xml version="1.0" encoding="utf-8"?>
<item android:id="#+id/abm_search"
android:icon="#android:drawable/ic_menu_search"
android:showAsAction="always|collapseActionView"
android:actionViewClass="android.widget.SearchView" />
<item android:id="#+id/abm_location"
android:title="Stuff"
android:showAsAction="never" />
<item android:id="#+id/abm_radius"
android:title="More Stuff"
android:showAsAction="never" />
<item android:id="#+id/abm_settings"
android:title="Other Stuff"
android:showAsAction="never"/>
Found out the problem. I had given the action bar a title of "". Instead, if you gave the action bar this display option:
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME);
it fixed the problem. Hope this helps someone else.

Android 3.0 ActionBar Gmail Search

In the Gmail app, the search bar moves all the way to the left when you click on the search icon. Does anyone know how to recreate this effect? The current way I have it, the icon expands when clicked, but doesn't move to the left like the Gmail app. Here is my menu xml file:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_search"
android:title="Search"
android:icon="#drawable/ic_menu_search"
android:showAsAction="ifRoom"
android:actionViewClass="android.widget.SearchView"
/>
</menu>
Take a look at this SO Question and see if that is what you're referring to:
Contextual Action Bar in Honeycomb
and
Contextual Action Bar in Honeycomb
The Gmail app seems to simply have a search icon in the menu and when you click on this it will add SearchView as the current navigation view with getActionBar.setCustomView().
There are a few gotchas when doing this, for example handling when it should be removed etc but if handled well it can lead to a nice user experience. But it may not be worth the hassle though, the regular expandable SearchView should be sufficient for most applications.

Categories

Resources