I was trying to create an action bar search in my application, but in the expanded state the SearchView is not taking the entire action bar width( it is still showing other actions)!
So, how to make the SearchView fill the full ActionBar Area (as in GMAIL app)?
None of the above solutions worked for me. Setting the MaxWidth of the SearchView worked for me:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_search, menu);
SearchView searchView = (SearchView)menu.findItem(R.id.menu_search).getActionView();
searchView.setMaxWidth(Integer.MAX_VALUE);
Got it
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_swipe_tabbed, menu);
MenuItem searchViewItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchViewItem.getActionView();
searchView.setIconifiedByDefault(false);
ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
searchView.setLayoutParams(params);
searchViewItem.expandActionView();
return true;
}
<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=".MySwipeTabbedActivity" >
<item android:id="#+id/action_search"
android:title="Search"
android:icon="#android:drawable/ic_menu_search"
android:showAsAction="always|collapseActionView"
android:actionViewClass="android.widget.SearchView"
/>
<item android:id="#+id/action_share"
android:title="Share"
android:icon="#android:drawable/ic_menu_share"
android:showAsAction="ifRoom" />
<item android:id="#+id/action_settings"
android:title="Settings"
android:icon="#android:drawable/ic_menu_preferences"
android:showAsAction="never" />
</menu>
[Optional] Avoid the searchView from collapsing:
searchViewItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return false; //never collapse
}
});
The Answer given by #Hiep is correct i followed those steps to get my solution.but i was using ActionbarCompact lib so it take some changes to made so that i get it to work This solution is only the modified answer of Hiep if you are using appcompact
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:materialdesign="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_search"
android:title="search"
android:icon="#drawable/abc_ic_search_api_mtrl_alpha"
materialdesign:showAsAction="always|collapseActionView"
materialdesign:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
And in the Main Class onCreateOptionsMenu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my_swipe_tabbed, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
searchView.setLayoutParams(params);
searchView.setIconified(false);
MenuItemCompat.expandActionView(searchItem);
return super.onCreateOptionsMenu(menu);
}
Avoid the searchView from collapsing:If u use the above method in Appcompact it will create a crash so use this solution to avoid that.
MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
/* (non-Javadoc)
* #see android.support.v4.view.MenuItemCompat.OnActionExpandListener#onMenuItemActionExpand(android.view.MenuItem)
*/
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
/* (non-Javadoc)
* #see android.support.v4.view.MenuItemCompat.OnActionExpandListener#onMenuItemActionCollapse(android.view.MenuItem)
*/
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return false;
}
});
Thank you
Try This Only will work and keep all other items set to ifRoom
<item
android:id="#+id/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="#string/action_search"
app:showAsAction="always"/>
Yes, I'm really late :)
But I would like to share one more alternative solution. The idea is just to hide the grouped menu items when the SearchView gets expanded.
Step 1:
Group your menu items. Please make sure showAsAction of search view item is app:showAsAction="collapseActionView|always"
<group android:id="#+id/myMenuGroup">
<item
android:id="#+id/actionSearch"
android:icon="#drawable/ic_search"
android:title="#string/search"
app:actionViewClass="android.widget.SearchView"
app:iconTint="#color/textColorVariant2"
app:showAsAction="collapseActionView|always" />
<item
android:id="#+id/actionFilter"
android:icon="#drawable/ic_filter"
android:orderInCategory="0"
android:title="#string/filter"
app:iconTint="#color/textColorVariant2"
app:showAsAction="ifRoom" />
..... </group>
Step 2: Get the menu reference in onPrepareOptionsMenu() callback
override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
super.onPrepareOptionsMenu(menu)
this.menu = menu // this.menu is a global scoped variable
return true
}
Step 3
Add MenuItem.OnActionExpandListener into your activity and implement onMenuItemActionExpand() and onMenuItemActionExpand() like below
override fun onMenuItemActionExpand(p0: MenuItem?): Boolean {
menu?.setGroupVisible(R.id.myMenuGroup, false)
return true
}
override fun onMenuItemActionCollapse(p0: MenuItem?): Boolean {
menu?.setGroupVisible(R.id.myMenuGroup, true)
return true
}
This approach will expand the SearchView completely inside the ActionBar even if there are other menu icons present
Related
I want to have a search view and two buttons in the Action Bar. So I created menu/options_menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_search"
android:title="Search"
android:icon="#drawable/ic_menu_search"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView" />
<item
android:title="Route"
android:id="#+id/action_route"
android:icon="#drawable/route"
android:showAsAction="always" />
<item
android:title="Cancel"
android:id="#+id/action_cancel"
android:icon="#drawable/cancel"
android:showAsAction="always" />
</menu>
I am inflating it in code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
return true;
}
This is what I get:
As you can see, only the first item ("route") is shown.
I can make second item ("cancel") visible if I let iconify the search widget:
//searchView.setIconifiedByDefault(false);
However, I want to have search widget expanded and both items visible. Is it possible?
Update
I managed to make them visible by setting android:orderInCategory="1" for "Route" and "Cancel" and android:orderInCategory="2" for the search widget:
This is almost what I wanted. The issue is that I want search widget to go first (leftmost), and then these two items. Is it possible?
Update 2
I tried "recommended" way. I set for SearchView:
android:showAsAction="always|collapseActionView"
After I click on search icon, I get expanded view:
Two problems:
(Minor) Navigation UI occupies a room on the left thus reducing space available for SearchView.
(Major) Android icon appears as a part of navigation UI. Can I get rid of it?
Created one demo, check it :
menu.xml :
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_search"
android:title="Search"
android:icon="#android:drawable/ic_search_category_default"
app:actionLayout="#layout/searchview"
app:showAsAction="always|collapseActionView"/>
<item
android:title="Route"
android:id="#+id/action_route"
android:icon="#mipmap/ic_launcher"
app:showAsAction="always" />
<item
android:title="Cancel"
android:id="#+id/action_cancel"
android:icon="#mipmap/ic_launcher"
app:showAsAction="always" />
</menu>
searchview.xml :
<?xml version="1.0" encoding="utf-8"?>
<SearchView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Java :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
if(searchItem!=null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
}
return true;
}
Edited Java Based on Requirement (Always open searchview) :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem searchItem = menu.findItem(R.id.action_search);
searchItem.expandActionView();
final SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
if(searchItem!=null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
// This listener use for handle back press if you want to go back without collapsing searchview
MenuItemCompat.setOnActionExpandListener(searchItem,new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
onBackPressed();
return false;
}
});
}
return true;
}
Screenshots :
I did it by setting a maximum width for the search view, equal to screen width, minus the action item width.
res/menu/search.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_search"
android:icon="#drawable/ic_search_white_24dp"
android:title="#string/title_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always" />
<item
android:id="#+id/action_filter"
android:icon="#drawable/ic_filter_list_white_24dp"
android:title="#string/title_filter"
app:showAsAction="always" />
</menu>
Activity or fragment:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.search, menu);
searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setIconified(false);
searchView.setMaxWidth(getSearchViewMaxWidth());
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
#Override
public boolean onClose() {
performSearch("");
return true; // Disable dismissing the search view
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
performSearch(query);
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
}
#Px
private int getSearchViewMaxWidth() {
final int screenWidth = getResources().getDisplayMetrics().widthPixels;
final int actionItemWidth = getResources().getDimensionPixelSize(R.dimen.action_item_width);
return screenWidth - actionItemWidth;
}
res/values/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- ... -->
<dimen name="action_item_width">48dp</dimen>
</resources>
Change your onCreateOptionsMenu as follows to explicitly set the menu items visible when the SearchView menu item has focus.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
if (menu.getItem(0).isFocused()) //detect if search view has focus
{
//explicitly set menu items visible
menu.getItem(1).setVisible(true);
menu.getItem(2).setVisible(true);
}
return true;
}
you can customize width of expanded search view by this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.MATCH_PARENT);
params.width=500;
searchView.setLayoutParams(params);
menu.findItem(R.id.action_search).expandActionView();
return true;
}
try this:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_search"
android:title="Search"
android:icon="#drawable/ic_menu_search"
android:orderInCategory="1"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
<item
android:title="Route"
android:id="#+id/action_route"
android:icon="#drawable/route"
android:orderInCategory="2"
android:showAsAction="always" />
<item
android:title="Cancel"
android:id="#+id/action_cancel"
android:icon="#drawable/cancel"
android:orderInCategory="2"
android:showAsAction="always" />
</menu>
Solution:
The problem was due to the usage of app:showAsAction="collapseActionView|always". Replacing this with app:showAsAction="always" solved the problem.
I have a Fragment with ViewPager and the ViewPager has 3 Fragments. Each Fragment has a SearchView in its Menu Items (Displayed in the Toolbar as Menu Item). When i click on the SearchView, i get the click event in onOptionsItemSelected, but the SearchView doesn't expand. Not sure what to do.
I have set setHasOptionMenu(true) in onCreate() for all the Fragments.
Here is my current code: for each Fragment
onCreateOptionsMenu:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_new, menu);
SearchManager searchManager = (SearchManager) activity.
getSystemService(Context.SEARCH_SERVICE);
searchMenuItem = menu.findItem(R.id.ic_action_search);
searchView = (SearchView) searchMenuItem.getActionView();
searchView.setSearchableInfo(searchManager.
getSearchableInfo(activity.getComponentName()));
searchView.setOnQueryTextListener(this);
}
Menu XML file:
<?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/ic_action_search"
android:icon="#drawable/ic_search_white_24dp"
android:orderInCategory="0"
android:title="Search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|always" />
<item
android:id="#+id/action_list"
android:icon="#drawable/ic_action_list_white"
android:orderInCategory="1"
android:title="#string/action_list"
android:checked="true"
android:checkable="true"
app:showAsAction="never" />
<item
android:id="#+id/action_grid"
android:icon="#drawable/ic_action_grid_white"
android:orderInCategory="2"
android:title="#string/action_grid"
android:checked="false"
android:checkable="true"
app:showAsAction="never" />
<item
android:id="#+id/action_mini"
android:icon="#drawable/ic_action_grid_white"
android:orderInCategory="3"
android:title="#string/action_mini"
android:checked="false"
android:checkable="true"
app:showAsAction="never" />
</menu>
Other related code: if it helps
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
mViewPagerPetitionsRecyclerViewAdapter.getFilter().filter(newText);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_list) {
prefs.edit().putString(Const.VIEWPAGER.VIEW_TYPE, Const.VIEWPAGER.LIST_VIEW).apply();
mStaggeredLayoutManager.setSpanCount(1);
} else if (item.getItemId() == R.id.action_grid) {
prefs.edit().putString(Const.VIEWPAGER.VIEW_TYPE, Const.VIEWPAGER.GRID_VIEW).apply();
mStaggeredLayoutManager.setSpanCount(2);
} else if (item.getItemId() == R.id.action_mini) {
prefs.edit().putString(Const.VIEWPAGER.VIEW_TYPE, Const.VIEWPAGER.MINI_VIEW).apply();
mStaggeredLayoutManager.setSpanCount(1);
}
ActivityCompat.invalidateOptionsMenu(activity);
mViewPagerPetitionsRecyclerViewAdapter.notifyDataSetChanged();
mIMainNewPetitionsListener.onLayoutChangedListener();
return super.onOptionsItemSelected(item);
}
#Override
public void onPrepareOptionsMenu(Menu menu) {
MenuItem list_item = menu.findItem(R.id.action_list);
MenuItem grid_item = menu.findItem(R.id.action_grid);
MenuItem mini_item = menu.findItem(R.id.action_mini);
String item_type = prefs.getString(Const.VIEWPAGER.VIEW_TYPE, Const.VIEWPAGER.LIST_VIEW);
if (item_type.equalsIgnoreCase(Const.VIEWPAGER.LIST_VIEW)) {
list_item.setChecked(true);
grid_item.setChecked(false);
mini_item.setChecked(false);
} else if (item_type.equalsIgnoreCase(Const.VIEWPAGER.GRID_VIEW)) {
list_item.setChecked(false);
grid_item.setChecked(true);
mini_item.setChecked(false);
} else if (item_type.equalsIgnoreCase(Const.VIEWPAGER.MINI_VIEW)) {
list_item.setChecked(false);
grid_item.setChecked(false);
mini_item.setChecked(true);
}
super.onPrepareOptionsMenu(menu);
}
I think you have to expand the action view yourself:
In onOptionsItemSelected:
if (item.getItemId() == R.id.action_search) {
item.expandActionVew();
}
I am using Appcompat v7 23.1.1. I have a fragment with ViewPager(FragmentStatePagerAdapter) loading 3 fragments. I have a SearchView with in each of these 3 fragments. When i click on the SearchView, the view didn't expand.
The problem was due to the usage of app:showAsAction="collapseActionView|always". Replacing this with app:showAsAction="always" solved the problem.
Not sure if this is a bug, but this post and this post helped me in solving this.
I am using appcompat v7 for searchview with toolbar except actionbar. below is my menu xml file and java file.
menu file:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always"/>
<item
android:id="#+id/action_sort"
android:icon="#drawable/ic_action_sort"
android:title="#string/sort"
app:showAsAction="ifRoom"/>
</menu>
java file:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.dashboard, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) MainActivity.this.getSystemService(Context.SEARCH_SERVICE);
if (searchItem != null) {
searchView = (SearchView) searchItem.getActionView();
}
if (searchView != null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(MainActivity.this.getComponentName()));
}
return super.onCreateOptionsMenu(menu);
}
now i want to collapse searchview if it is expanded otherwise want to work backpressed on backpressed() method. how can i achieve this.?
Collapsing on backpressed is handled by default in my own setup. I didn't implement a custom onBackPressed method. I did nothing special except extending from ActionBarActivity.
I used MenuItemCompat to get actionview, that might do the trick.
This is my menu xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.yourapp.youractivity">
<item
android:id="#+id/search"
android:title="#string/app_name"
android:icon="#drawable/nav_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
This is how i create the menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.shop_list, menu);
SearchManager searchManager =
(SearchManager)getSystemService(Context.SEARCH_SERVICE);
//Using MenuItemCompat here, that can do the trick
searchView =
(SearchView)MenuItemCompat.
getActionView(menu.findItem(R.id.search));
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
//etc...
//etc...
return true;
}
In onBackPressed call invalidateOptionsMenu() or store SearchView as Activity field and call searchView.onActionViewCollapsed(); but that can require additional work when restoring your Toolbar state (title state etc.).
Try this inside the Class and extend ActionBarActivity in your Class
(Example: - public class Home extends ActionBarActivity)
#Override
public void onBackPressed() {
super.onBackPressed()
}
For collapsing the SearchView: Try this,
menu.collapseActionView();
(or)
searchView = menuItem.getActionView().
(or)
searchView.onActionViewCollapsed()
I'm trying to implement SearchView according to some online tutorials. But when i clicked the search icon, it did not expand, instead another search icon appeared to the left and i must click again; this time it worked.
Are there something i'm missing here?
Here are the captured images, sorry i can't post these to stackoverflow yet.
http://1drv.ms/186yI2Y
If i delete collapseActionView then it worked, but i can't customize search icon.
<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">
<item android:id="#+id/search"
android:title="#string/search_title"
android:icon="#drawable/ic_action_search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.widget.SearchView"/>
</menu>
In MainActivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem searchItem = menu.findItem(R.id.search);
mSearchView = (SearchView) searchItem.getActionView();
mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
mSearchView.setIconifiedByDefault(true);
mSearchView.setOnSearchClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showResultsFragment();
}
});
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
boolean toggleHandled = mDrawerToggle.onOptionsItemSelected(item);
return toggleHandled || super.onOptionsItemSelected(item);
}
Thank in advance!
in your menu xml i changed app:actionViewClass="android.support.v7.widget.SearchView"
<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">
<item android:id="#+id/search"
android:title="wrwrwr"
android:icon="#drawable/abc_ic_menu_copy_mtrl_am_alpha"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
and in your activity
import android.support.v7.widget.SearchView; //don't import android.widget.SearchView
I am trying to add actions items in my action bar using appcompat. I added search action when I click on it my application crash with NullPointerException. In onCreateOptionsMenu I am getting this view reference but there I am always getting null. Any suggestion to resolve this ??
main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_bar_refresh"
android:icon="#drawable/ic_action_refresh"
app:showAsAction="ifRoom"
android:title="#string/action_refresh" />
<item
android:id="#+id/action_bar_search"
android:icon="#drawable/ic_action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom"
android:title="#string/action_search" />
<item
android:id="#+id/action_bar_settings"
android:title="#string/action_settings"
app:showAsAction="never" />
</menu>
MainActivity.java
public class MainActivity extends ActionBarActivity {
private SearchView mSearchView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
MenuItem searchItem = menu.findItem(R.id.action_bar_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_bar_search:
mSearchView.setIconified(false);
return true;
}
return false;
}
}
Here I am getting null value in searchView
MenuItem searchItem = menu.findItem(R.id.action_bar_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
Screenshot
<item
android:id="#+id/action_bar_search"
android:icon="#drawable/ic_action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|ifRoom"
android:title="#string/action_search" />
You change showAsAction attribute.
I fixed issue by setting Application Theme to Theme.AppCompat.Light
<style name="AppBaseTheme" parent="#style/Theme.AppCompat.Light">