Android 3.0 honeycomb search view on action bar - android

How to have a 'search item' on the action bar for honeycomb? If possible please provide full code with layout.

I'm assuming you mean adding the search widget to the actionbar?
Here's how it's done:
<?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>
This is of course placed in the res/menu folder.
You add this to your actionbar just like any adding any other menu.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
// Set appropriate listeners for searchView
...
return super.onCreateOptionsMenu(menu);
}
This was all taken from here: http://developer.android.com/guide/topics/ui/actionbar.html#ActionView

Related

Menu item goes in dropdown list menu not in the action bar

Hello
I'm trying to add 2 menu items in the action bar. On the designer they look ok, but when I run the application, both menu items goes in the dropdown list hamburger menu (there's enough "room" to display on the action bar).
I tried to replace app:showAsAction to android:showAsAction, doesn't work this replacement.
this is my menu_main.xml
<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="com.tabdemo.MainActivity">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title=""
app:showAsAction="never"
/>
<item
android:id="#+id/userMenu"
android:title="User"
app:showAsAction="ifRoom" />
<item
android:id="#+id/logoutMenu"
android:icon="#drawable/opendoorlogo2"
android:title="Logout"
app:showAsAction="ifRoom" />
And this is the java code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
MenuItem userMenuItem = menu.findItem(R.id.userMenu);
userMenuItem.setTitle(username);// global string
MenuItem logoutMenuItem = menu.findItem(R.id.logoutMenu);
logoutMenuItem.setIcon(R.drawable.opendoorlogo2);
return true;
}
Thanks in advance (P.S. Logo doesn't load on Logout menu Item)
First of all make sure your drawable file is not too big for the actionbar, if so you can convert it to actionbar icon size.
https://romannurik.github.io/AndroidAssetStudio/icons-actionbar.html#source.space.trim=1&source.space.pad=0&name=ic_action_example&theme=light&color=33b5e5%2C60
android:showAsAction="always"
if this doesn't help you.
Best way is to create a custom actionbar layout and place your icon over there instead of adding it as Menus.

SearchView widget isn't showing up on the ActionBar

I have been trying to put a SearchView widget on the Action bar, but it isn't showing up. I have tried the tutorial from google http://developer.android.com/training/search/setup.html but nothing happens to the Action bar. Here is the code for further reference.
options_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/search"
android:title="#string/search_title"
android:icon="#drawable/ic_search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
</menu>
This is the code that I wrote in the Jave file of the Activity I want the SearchView widget in.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true;
}
Minimum sdk = 14
Please Help!

How to get menu options in actionbar aligned with fragments?

On a tablet in landscape, the GMail app has the following:
The magnifying glass menu item appears to be aligned to the right side of the list fragment.
How can I achieve this in my own app?
Step I: Create a xml in the res/menu folder.
Step II: Add the following code snippet in the menu_main.xml or the pre-provided menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_search"
android:title="#string/action_search"
android:icon="#drawable/ic_action_search_hdpi"
yourapp:showAsAction="always"
android:enabled="true"
yourapp:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
`
Step III: Inflate the menu.xml in the onCreateOptionsMenu(Menu menu)
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.menu_main, menu);
return true;
}
Since Search is also a menu option, android already provides the widget to use it seamlessly.You can also handle a click listener to integrate the search mechanism into the app with the help of API's.
I hope it helps.

Search Actionview not collapsing in single up button click android actionbar compat

I am using actionbar compat support library to provide search widget in Action Bar. The search action view is collapsing only after pressing back button or up button twice. I had same problem while executing the sample dictionary search also.
Code in my search activity which extends ActionBarActivity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu
getMenuInflater().inflate(R.menu.menu_main, menu);
// Find the search item
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
return super.onCreateOptionsMenu(menu);
}
My Searchable.xml looks like this
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/search_label"
android:hint="#string/search_hint"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
>
Let me know if any more information needed...
Check the API guide http://developer.android.com/guide/topics/ui/actionbar.html#ActionView This is what it should look for anything less than API 11.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
yourapp:orderInCategory="9"
yourapp:menuCategory="system"
yourapp:showAsAction="always|collapseActionView"
yourapp:actionViewClass="android.widget.SearchView"
android:title="#string/action_searchSt" />
</menu>
That's probably more attributes than you need (menuCategory and orderInCategory are unnecessary). But assuming you've added in the support library correctly and you read the link I gave, things should work as intended.

Force action Bar show search view

I am able to expand search view by action like this
<item android:id="#+id/menu_search"
android:title="Search"
android:showAsAction="never|collapseActionView"
android:actionViewClass="android.widget.SearchView" />
But i have a 3-tab activity and i'd like to SearchView be always expanded
How may I do that?
Two steps are necessary.
First, you have to make sure your search menu item is always shown as an action and never moved into the overflow menu. To achieve this set the search menu item's showAsAction attribute to always:
<item
android:id="#+id/menu_search"
android:title="Search"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView" />
Second, make sure the action view is not shown in iconified (i.e. collapsed) mode by default. To do this call setIconifiedByDefault(false) on your search view instance:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_activity, menu);
MenuItem searchViewItem = menu.findItem(R.id.menu_search);
SearchView searchView = (SearchView) searchViewItem.getActionView();
[...]
searchView.setIconifiedByDefault(false);
return true;
}
That should do it.

Categories

Resources