I get NPE on SearchView.OnQueryTextListener.
/res/menu/order_toolbar.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/order_menu_search"
android:icon="#drawable/ic_search"
android:title="#string/order_menu_search"
app:actionViewClass="android.support.widget.v7.SearchView"
app:showAsAction="ifRoom|withText" />
<item
android:id="#+id/order_menu_checkmark"
android:icon="#drawable/ic_checkmark"
android:title="#string/order_toolbar"
app:showAsAction="ifRoom|withText" />
ActivityOrder.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.order_toolbar, menu);
mMenuCheckmark = menu.findItem(R.id.order_menu_checkmark);
mMenuCheckmark.setVisible(false);
return super.onCreateOptionsMenu(menu);
}
FragmentOrder.java
#Override
public void onPrepareOptionsMenu(Menu menu){
MenuItem menuItem = menu.findItem(R.id.order_menu_search);
SearchView searchView = (SearchView) menuItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
Toast.makeText(getActivity(), "" +newText, Toast.LENGTH_SHORT).show();
return false;
}
});
}
I'm inflating menu in Activity (i need to hide checkmark and enable it later after the user made some input, all the logic is in the activity), and in Fragment, I have RecyclerView and I need to implement search (in the toolbar). So basically, I need to access that SearchView INSIDE FRAGMENT only.
I have been trying to find anything but nothing helps, if I inflate menu in fragment then I get doubled icons in the toolbar.
setHasOptionsMenu(true);
is already added in onCreate
Also i tried this:
1. inflate the menu in fragment (removed onCreateOptionsMenu in activity)
2. passed menuItem (checkmark) back to activity trough interface so i can access checkmark
3. still getting NPE if i set listener on searchview
I make option menu in fragment. Like this,
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
...
setHasOptionsMenu(true);
...
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.fragment_menu, menu);
menu.findItem(R.id.menu_1).setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_bar_search));
searchView = (SearchView) menu.findItem(R.id.menu_1).getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s) {
return false;
}
#Override
public boolean onQueryTextChange(String s) {
return false;
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
#Override
public boolean onClose() {
return false;
}
});
....
}
Not sure, you call "searchView.setOnQueryTextListener" from onCreateOptionsMenu.
Solved:
mMenuSearch = (SearchView) menu.findItem(R.id.order_menu_search).getActionView();
mMenuSearch.setOnQueryTextListener(this);
added implements SearchView.OnQueryTextListener on Fragment and 2 new Override methods onQueryTextSubmit and onQueryTextChanged
Related
I am trying to implement a search Filter in my Fragment, but it does not let me. I used the same code in Activity before and it worked. Whenever I type something, the onCreateOptionsMenu() does not get called. The App looks like this : [![app Picture][1]][1]
As you can see I type something in the top and it won't get filtered.
My Fragment code: `
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
setMenuVisibility(false);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View RootView = inflater.inflate(R.layout.fragment_tab1, container, false);
if(getArguments() != null){
String yourText = getArguments().getString("interessen");
System.out.println(yourText);
}`
.
.
.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menuevents, menu);
super.onCreateOptionsMenu(menu, inflater);
MenuItem searchitem = menu.findItem(R.id.searchEvent);
//searchitem.setVisible(false);
final SearchView searchView = (SearchView) searchitem.getActionView();
final List<Event> allEvents = new ArrayList<>();
allEvents.addAll(eventList);
searchitem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
eventList.clear();
eventList.addAll(allEvents);
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
eventList.clear();
eventList.addAll(allEvents);
return true;
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
eventListAdapter.getFilter().filter(newText);
return false;
}
});
I did not copy the EventListAdapter in, becuase this should work just fine. Its just that the Fragment does not get Access to the SearchItem.
Anybody? :)
[1]: https://i.stack.imgur.com/JkLcL.png
You've called setMenuVisibility(), which means your Fragment is specifically opting out of having its menu being shown. It is expected that when your menu isn't shown, onCreateOptionsMenu() is not triggered.
Remove that line of code to have your menu actually be visible.
I have a toolbar into fragment and i want to add searchView for this toolbar
I have this:
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
getActivity().getMenuInflater().inflate(R.menu.menu_principal, menu);
MenuItem item = menu.findItem(R.id.buscar);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
try {
ArrayList<Producto> listaFiltrada = filter(list,newText);
adapterProductos.setFilter(listaFiltrada);
}catch (Exception r) {
r.printStackTrace();
}
return true;
}
});
MenuItemCompat.setOnActionExpandListener(item, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
adapterProductos.setFilter(list);
return true;
}
});
}
and this item menu:
<item android:title="Buscar"
android:id="#+id/buscar"
android:icon="#drawable/ic_search_white_24dp"
app:actionViewClass= "android:support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView"
/>
But i have a null pointer...
FATAL EXCEPTION: main
Process: jaimeperezpedraza.org.tienda, PID: 15971
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SearchView.setOnQueryTextListener(android.widget.SearchView$OnQueryTextListener)' on a null object reference
I think I'm not catching searchView well...
help?
Your NullPointerException is due to the searchView being null. This is because your XML has app:actionViewClass="android:support.v7.widget.SearchView" - note the : in the middle of the class name.
It should instead be android.support.v7.widget.SearchView:
<item android:title="Buscar"
android:id="#+id/buscar"
android:icon="#drawable/ic_search_white_24dp"
app:actionViewClass= "android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView"
/>
I have an activity which is containing fragment, then in the fragment i have toolbar which is containing SearchView. I want to override activity's onBackPressed() to close the searchView's focus. But i didn't found how to get the state of fragment's menu. Here's the code :
MainActivity.java
#Override
public void onBackPressed() {
if (currentFragment instanceof FragmentListProduct) {
DrawerLayout drawerLayout = (DrawerLayout) currentFragment.getView().findViewById(R.id.drawer_layout);
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START);
} /*if currentFragment.searchViewIsNotClosed {
closeSearchView();
} */ else if (backstackFragment.size() > 0) {
replaceFragment(null, backstackFragment.pop());
} else {
super.onBackPressed();
}
} else {
super.onBackPressed();
}
}
FragmentListProduct.java
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_main, menu);
MenuItem searchItem = menu.findItem(R.id.app_bar_search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {...});
super.onCreateOptionsMenu(menu, inflater);
}
So how to get fragment's menu in activity so i can access its state?
You can expose the view from the onCreateOptionsMenu() via the fragment and use isIconified() to check the status and setIconified (true) to close it.
FragmentListProduct.java
private SearchView searchView;
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
//...
searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
//...
}
public SearchView getSearchView() {
return searchView;
}
MainActivity.java
public void onBackPressed() {
//...
SearchView searchView = ((FragmentListProduct)currentFragment).getSearchView();
if (searchView != null) {
if (searchView.isIconified()) {
// the search is extended
searchView.setIconified(true);
} else {
// the search view is closed
}
}
/...
}
I need to have oncloselistener for my searchview in a menu, but oncloselistener is not working. I am not sure how to set actionexpand to my searchview.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
final SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setQueryHint("جستجو...");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
I had the same issue last week! To my surprise, you don't set the listener to the SearchView, you actually set it to the menu item that expands it.
This is exactly what I had to do in onCreateOptionsMenu:
item = menu.findItem(R.id.friend_fragment_search_icon);
searchView = (SearchView) MenuItemCompat.getActionView(item);
// When using the support library, the setOnActionExpandListener() method is
// static and accepts the MenuItem object as an argument
MenuItemCompat.setOnActionExpandListener(item, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
//the searchview has been closed
return true; // Return true to collapse action view
}
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
mBtnFindFriends.hide();
return true; // Return true to expand action view
}
});
The friend_fragment_search_icon is the menu icon that opens the SearchView.
menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dingding="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/friend_fragment_search_icon"
android:title="#string/search_title"
android:icon="#drawable/ic_search_white_48dp"
dingding:showAsAction="always|collapseActionView"
dingding:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
I am trying to populate search and listview from text file and am having issues with this code can someone please help me This is the error message in the log cat
java.lang.NullPointerException at searchviewsqlite.MyActivity.onCreateOptionsMenu"
This is my code:
public class MyActivity extends ActionBarActivity {
Menu m;
final Context context=this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
DatabaseTable db=new DatabaseTable(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
SearchManager searchManager =(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));//Error here
}
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
//return super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.search:
onSearchRequested();
return true;
default:
return false;
}
}
}
This is my menu code below
<item
android:id="#+id/search"
android:title="Search"
android:icon="#drawable/search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView"/>