I have a searchview next to a filter button, but ever time I click on the other button the searchview also listens and expands, here is a snippet of the before How the buttons are positioned
When filter button is clicked
and here is my menu layout:
<?xml version="1.0" encoding="utf-8"?>
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_search_black_24dp"
android:title="#string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView" />
<item
android:id="#+id/action_sort"
android:icon="#drawable/ic_filter_list_black_24dp"
android:title="#string/Sort"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView" />
</menu>
my Java code:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.activty_ecommerce_sort, menu);
SearchManager manager = (SearchManager) getContext().getSystemService(Context.SEARCH_SERVICE);
final SearchView search = (SearchView) menu.findItem(R.id.action_search).getActionView();
search.setSearchableInfo(manager.getSearchableInfo(getActivity().getComponentName()));
search.setActivated(true);
search.setQueryHint("Search");
search.onActionViewExpanded();
search.setIconified(false);
search.clearFocus();
search.setOnQueryTextListener(this);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
int id = menuItem.getItemId();
switch (id){
case R.id.action_sort:
filterDailog();
break;
}
return super.onOptionsItemSelected(menuItem);
}
Anyone have an idea where the issue is?
You need to remove app:actionViewClass="android.support.v7.widget.SearchView" from sort item.
<item
android:id="#+id/action_sort"
android:icon="#drawable/ic_filter_list_black_24dp"
android:title="#string/Sort"
app:showAsAction="ifRoom|collapseActionView" />
instead of;
<item
android:id="#+id/action_sort"
android:icon="#drawable/ic_filter_list_black_24dp"
android:title="#string/Sort"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView" />
Related
I have a searchview in toolbar in a fragment but when I click on the search item I want the other menu items to hide and searchview to take the full width. But the menu items don't hide when the searchview is clicked which is very inconvenient. I tried to use searchView.setMaxWidth(Integer.MAX_VALUE); but it didn't work.
toolbar.inflateMenu(R.menu.files_menu);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
SearchView searchView = (SearchView) item.getActionView();
searchView.setQueryHint("Search");
searchView.setMaxWidth(Integer.MAX_VALUE);
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus){
toolbar.setBackgroundColor(FilesFragment.this.getResources().getColor(R.color.grey));
} else {
toolbar.setBackgroundColor(FilesFragment.this.getResources().getColor(R.color.light_red));
toolbar.collapseActionView();
}
This the files_menu
<?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/search"
android:title="search"
android:icon="#drawable/ic_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="androidx.appcompat.widget.SearchView"
/>
<item
android:id="#+id/go"
android:icon="#drawable/ic_baseline_go_24"
app:showAsAction="ifRoom|collapseActionView"
android:title="go"/>
<item
android:icon="#drawable/ic_baseline_more_vert_white"
android:id="#+id/menu_overflow"
app:showAsAction="ifRoom|collapseActionView"
android:title="TODO">
<menu>
<item
android:title="Find"
android:id="#+id/find"
/>
</menu>
</item>
</menu>
I also tried to define the action layout in menu xml but that did not work as well.
<item android:id="#+id/search"
android:title="search"
android:icon="#drawable/ic_search"
android:actionLayout="#layout/searchview"
app:showAsAction="ifRoom|collapseActionView"
/>
layout/searchview.xml is
<androidx.appcompat.widget.SearchView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxWidth="10000dp" />
I think it is duplicated question, you can check the following questions and the answers
Expand Search View to use entire Action Bar (hide other things)
I am trying to build a searchview in the toolbar ion my android app, but am getting a null pointer exception in the 3rd line of onCreateoptionsMenu method. Any help will be appreciated, thank you in advance.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
SearchManager searchManager =(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
Here is my menu.xml -
<item
android:icon="#drawable/ic_search"
android:id="#+id/action_search"
android:orderInCategory="200"
android:title="Search"
app:showAsAction="ifRoom" />
Set SearchView class in menu.
<item
android:icon="#drawable/ic_search"
android:id="#+id/action_search"
android:orderInCategory="200"
android:title="Search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom" />
You missd app:actionViewClass="android.support.v7.widget.SearchView" attribute.
And you need to import android.support.v7.widget.SearchView;
<?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/search"
android:title="#string/Search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always|collapseActionView" />
</menu>
You forgot to add actionViewClass in menu.xml
Add these line to you menu.xml
...
app:showAsAction="always|collapseActionView"
android:actionViewClass="android.support.v7.widget.SearchView" />
While in your Java code find SearchView using -
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
Reference - MenuItemCompat
Try to use this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
// Inflate menu to add items to action bar if it is present.
inflater.inflate(R.menu.menu_main, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appcompat="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/tools">
<item
android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never"/>
<item
android:id="#+id/menu_search"
android:title="#string/menu_search"
appcompat:actionViewClass="android.support.v7.widget.SearchView"
appcompat:showAsAction="always"/>
Try changing this line:
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
I'm using Toolbar and SearchView for the ActionBar. After submit query text, It's changed like the picture below. How to set OnClick to the back button?
Edit 1: Here are involve code
menu.xml
<item
android:id="#+id/action_search"
android:orderInCategory="100"
android:title="#string/action_settings"
android:icon="#drawable/ic_search_white"
app:showAsAction="always|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"/>
In my Fragment class
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_main, menu);
final MenuItem myActionMenuItem = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(myActionMenuItem);
final ImageView mCloseButton = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(final String query) {
if (!searchView.isIconified()) {
searchView.setIconified(true);
}
mCloseButton.performClick();
return false;
}
...
Simply add android:showAsAction="always|collapseActionView" in your menu item.
Example menu.xml
<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_search"
android:icon="#drawable/ic_search_white_24dp"
android:showAsAction="always|collapseActionView"
android:actionViewClass="android.widget.SearchView" />
</menu>
I was trying to create a menu, and add some actions "ifRoom" using code below:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_search"
android:icon="#android:drawable/ic_menu_search"
android:actionViewClass="android.widget.SearchView"
android:showAsAction="ifRoom|collapseActionView"
android:title="#string/search_description" />
<item
android:id="#+id/menu_refresh"
android:title="#string/menu_refresh"
android:showAsAction="ifRoom|withText"
/>
<item
android:id="#+id/menu_preference"
android:title="#string/menu_preferences"
android:showAsAction="never"
/>
</menu>
But what i got is the menu is displaying at the bottom...
I want it to be at top of the screen...
Any idea what happened here? Thanks!
Screenshot link: http://i.stack.imgur.com/knECg.png
Below is the MainActivity.java onCreateOptionaMenu method:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
// Moved from onCreate -- Retrieve the Search View and configure/enable it
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()) );
return true;
}
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">