I'm using SearchView (android.support.v7.widget.SearchView) to filter items in my listview. I have a listview which includes items with a checkbox (the checkbox is a marker to select the item). When i click the checkbox, i call invalidateOptionsMenu(). Then there appears a new menu item which can do some things with the "selected" items. Here some Code:
SearchView in menu.xml:
<item
android:id="#+id/search_movie_icon"
android:icon="#drawable/ic_search_white_24dp"
android:title="#string/search_title"
hmkcode:actionViewClass="android.support.v7.widget.SearchView"
hmkcode:showAsAction="always|collapseActionView"/>
Fragment:
#Override
public void onCreateOptionsMenu(Menu p_Menu, MenuInflater p_Inflater) {
p_Menu.clear();
p_Inflater.inflate(R.menu.movie_actionbar, p_Menu);
m_SearchView = (SearchView) MenuItemCompat.getActionView(p_Menu.findItem(R.id.search_movie_icon));
m_SearchView.setIconified(false);
m_OnQueryTextListener = new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String p_NewText) {
m_MovieManager.setFilter(p_NewText);
// this call will get the new movie list from the moviemanager and update the view
updateViews();
return false;
}
#Override
public boolean onQueryTextSubmit(String p_Submit) {
// do nothing -> this will be called on pressing enter
return false;
}
};
m_SearchView.setOnQueryTextListener(m_OnQueryTextListener);
//some more unimportant stuff
}
Checkbox in listadapter:
l_ViewHolder.m_cbMarked.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View p_View) {
l_Movie.setMarked(((CheckBox) p_View).isChecked());
// This should update the optionsmenu to see the new menu item
((Activity) m_Context).invalidateOptionsMenu();
}
}
My problem is that the call invalidateOptionsMenu() removes the string in my searchview.
I know that there were some changes in SearchView:
Before:
#Override
public void onActionViewCollapsed() {
clearFocus();
updateViewsVisibility(true);
mQueryTextView.setImeOptions(mCollapsedImeOptions);
mExpandedInActionView = false;
}
Now:
#Override
public void onActionViewCollapsed() {
setQuery("", false);
clearFocus();
updateViewsVisibility(true);
mQueryTextView.setImeOptions(mCollapsedImeOptions);
mExpandedInActionView = false;
}
I tried some workarounds:
StateFulSearchView from SearchView in ActionBar -- problems with the *Up* button
Here i have some focus and icon problems...
A callback which removes my OnChangeTextListener before calling invalidateOptionsMenu() and readd it after this call (to avoid the filter actions) -> it didn't work...
Safe the filter before calling invalidateOptionsMenu() and set the old filter after that call -> then i can clearly see the flicker effect in my listview :(
I can't find a good solution...
Can someone help me? Let me know if you have questions.
Related
I'm getting mad with using SearchView in a fragment and the back button is always shown whatever i touched the search bar or not and i do the search but i have a problem that back button do nothing but clear the text in search i would like to use it to switch also between fragments. I've tried many solutions in stack overflow questions but seems not solving my issue.
the code after adding toolbar in the fragment:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.search_menu_fragment, menu);
MenuItem searchItem = menu.findItem(R.id.action_search_locations);
SearchView searchView = new SearchView(((AppCompatActivity)getActivity()).getSupportActionBar().getThemedContext());
MenuItemCompat.setShowAsAction(searchItem, searchItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW ); //searchItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW |
MenuItemCompat.setActionView(searchItem, searchView);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
// filter recycler view when query submitted
locationStatesPreviewAdapter.getFilter().filter(query);
return false;
}
#Override
public boolean onQueryTextChange(String query) {
// filter recycler view when text is changed
Log.e("QueryChange",query);
locationStatesPreviewAdapter.getFilter().filter(query);
return false;
}
});
}
the xml file has only the search item in the 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/action_search_locations"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:title="#string/app_name"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="always|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
Thanks.
That was a problem of mis-understanding, as i thought it's the back button of the SearchView in Fragment and afterwards it was the toolbar Navigation button and after adding this lines to make it responsive and all things done ! thanks for help all.
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("TOOLBAR");
toolbar.setNavigationOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Toast.makeText(getActivity(), "Back clicked!", Toast.LENGTH_SHORT).show();
}
});
I have this code
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
openSearch();
return true;
}
});
into onCreate( ) of my Activity.
OpenSearch function call opens up google now like search view. This only happens when the user clicks on the search action item in the toolbar. In my case I want the search view to open up automatically when the activity starts. How can this menu item click be done programmatically.
I can't call openSearch directly because it needs the menu to be created.
Is there there any callbacks informing that action menus have been created ?
You can try something like this :
tollbarLayout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
tollbarLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
tollbar.performclick();
}
}
});
I have a searchView in my SearchActivity and it creates an intent to launch a SearchActivityResults according to the documentation here: http://developer.android.com/training/search/setup.html
How do I need to go if I want my SearchView to trigger a search from within my activity, when the soft keyboard lower right button is pressed to trigger the search?
you just need to get your searchView menu , use override onCreateOptionsMenu
SearchView searchView = (SearchView) MenuItemCompat
.getActionView(mSearchMenuItem);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s) {
return false;
}
#Override
public boolean onQueryTextChange(String s) {
// do your search on change or save the last string in search
return false;
}
});
// you can get query
searchView.getQuery();
I have a problem.
I implemented a navigation drawer with a listView, and I want disable a item of this listview. I want that the user see the item disable, because it take a different color, and you can't clicked it.
The problem, only the activity knows when it be disabled.
an obvious example of a button:
Button but =...
but.setEnables(false);
and now the button change the colour, and you can't click it.
I want do the same for an element of listView.
I tryed this:
nameListView.getChildAt(position).setEnabled(false);
but this don't work.
What can i do?
Thanks.
How about using Adapter? Like this:
nameListView.getAdapter().getItem(position).setEnabled(false);
My solution:
In the class who represent the item of navigation drawer, I add a boolean attribute (enable)
public class NavDrawerItem {
....
private boolean enable;
.....
public boolean isEnable() {
return enable;
}
public void setEnable(boolean enable) {
this.enable = enable;
}
}
And in my adapter, override te method isEnable(position):
#Override
public boolean isEnabled(int position) {
return getItem(position).isEnable();
};
Now, in my activity, when the navigation drawer is open, execute this method:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
NavDrawerItem itemToDisable = (NavDrawerItem) mDrawerList.getAdapter().getItem(0);
itemToDisable.setEnable(centroComercial!=null); //Condition to disable item
return super.onPrepareOptionsMenu(menu);
}
Before this method, system "redrawer" navigation drawer, and now the item isn't enable.
The only thing that does not change color, for more usability.
I have five action menu items in the action bar, which I'm using action bar sherlock library as follows :
In onCreateOptionsMenu(), i used the following code :
menu.add(0,1,0 "Settings")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(0,2,0 "Favorites")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(0,3,0 "List")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(0,4,0 "Upload")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(0,5,0 "Search")
.setActionView(R.layout.search_layout)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
Now my Problem is Search Edit text (which is in red color) appears like this:
I want to make it to a full view in the action bar, like this :
This question is a bit old but I'll try to answer it anyway. I hope someone will find it useful.
Here is what I have done:
Create a custom SearchView class that extends SearchView. It defines 2 events: one fired when the search view expands and the other when it collapses.
public class EnglishVerbSearchView extends SearchView {
OnSearchViewCollapsedEventListener mSearchViewCollapsedEventListener;
OnSearchViewExpandedEventListener mOnSearchViewExpandedEventListener;
public EnglishVerbSearchView(Context context) {
super(context);
}
#Override
public void onActionViewCollapsed() {
if (mSearchViewCollapsedEventListener != null)
mSearchViewCollapsedEventListener.onSearchViewCollapsed();
super.onActionViewCollapsed();
}
#Override
public void onActionViewExpanded() {
if (mOnSearchViewExpandedEventListener != null)
mOnSearchViewExpandedEventListener.onSearchViewExpanded();
super.onActionViewExpanded();
}
public interface OnSearchViewCollapsedEventListener {
public void onSearchViewCollapsed();
}
public interface OnSearchViewExpandedEventListener {
public void onSearchViewExpanded();
}
public void setOnSearchViewCollapsedEventListener(OnSearchViewCollapsedEventListener eventListener) {
mSearchViewCollapsedEventListener = eventListener;
}
public void setOnSearchViewExpandedEventListener(OnSearchViewExpandedEventListener eventListener) {
mOnSearchViewExpandedEventListener = eventListener;
}
}
In my activity, I did the following:
- Created a private field to store the reference to the menu.
- Defined the events for collapsing and expanding the search view where I hide and show other actions by using the reference to the menu.
public class DictionaryActivity extends ActionBarActivity {
private Menu mMenu;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.dictionary, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
EnglishVerbSearchView searchView = (EnglishVerbSearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnSearchViewCollapsedEventListener(listenerCollapse);
searchView.setOnSearchViewExpandedEventListener(listenerExpand);
mMenu = menu;
return super.onCreateOptionsMenu(menu);
}
final private OnSearchViewCollapsedEventListener listenerCollapse = new OnSearchViewCollapsedEventListener() {
#Override
public void onSearchViewCollapsed() {
// show other actions
MenuItem favouriteItem = mMenu.findItem(R.id.action_favourite);
MenuItem playItem = mMenu.findItem(R.id.action_play);
favouriteItem.setVisible(true);
playItem.setVisible(true);
// I'm doing my actual search here
}
};
final private OnSearchViewExpandedEventListener listenerExpand = new OnSearchViewExpandedEventListener() {
#Override
public void onSearchViewExpanded() {
// hide other actions
MenuItem favouriteItem = mMenu.findItem(R.id.action_favourite);
MenuItem playItem = mMenu.findItem(R.id.action_play);
favouriteItem.setVisible(false);
playItem.setVisible(false);
}
};
}
In the menu.xml file, I used the custom search view:
<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/action_search"
yourapp:showAsAction="always|collapseActionView"
yourapp:actionViewClass="com.xxx.EnglishVerbSearchView" />
</menu>
I hope it's all that's needed as it's just an extract from my full code of the activity. Let me know if there's anything missing.
As per comment from arne.jans:
It seems to be enough to do MenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); to make room for the SearchView. The advantage would be: the other menu-items go into the overflow menu and are still accessible, even with the opened SearchView.