Adding a progressbar inside searchview android - android

This is below code I am using in my activity.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(com.c2info.eDawa.R.layout.hll_layout);
AppController.logite("Activity", "HllActivity");
final Toolbar toolbar = (Toolbar) findViewById(com.c2info.eDawa.R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.mipmap.app_launcher);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(com.c2info.eDawa.R.menu.menu_hll, menu);
final MenuItem searchItem = menu.findItem(com.c2info.eDawa.R.id.action_search);
searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
if (searchView != null)
searchView.clearFocus();
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
if (newText.length() > 2) {
getSearchData(newText); // this is the service
}
}
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
In on preExecute i am using
this.setProgressBarIndeterminateVisibility(Boolean.TRUE);
In on postexecute i am using
this.setProgressBarIndeterminateVisibility(Boolean.FALSE);
But progressbar is not showing can anyone help. Thanks in advance.

Related

SearchView :Triggering onQueryTextChange on Collapsing of the searchView

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
search(newText);
return true;
}
});
I don't want search(newText) to be executed when searchview collapses.
I tried
if(searchView.isShown) search(newText);
and
if(!searchView.isIconified) search(newText);
but when I close the searchview, searchView.isShown and searchView.isIconified values will be updated after triggering onQueryTextChange.
My onCreateOptionMenu code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_contacts, menu);
addContactMenuItem = menu.findItem(R.id.action_add_contact);
searchViewMenuItem = menu.findItem(R.id.action_search);
searchView = (SearchView) MenuItemCompat.getActionView(searchViewMenuItem);
MenuItemCompat.setOnActionExpandListener(searchViewMenuItem, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
addContactMenuItem.setVisible(false);
if (contactListAdapter != null)
contactListAdapter.getFilter().filter(searchView.getQuery());
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
addContactMenuItem.setVisible(true);
contactListAdapter.performGroupBasedFiltering(groupSpinner.getAdapter().getItemId(groupSpinner.getSelectedItemPosition()));
return true;
}
});
return true;
}
Please help me out! Thanks in advance.
Try this use setOnActionExpandListener
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_menu, menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(this);
MenuItemCompat.setOnActionExpandListener(item,
new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
// perform your action here
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
// perform your action here
return true;
}
});
return true;
}
EDIT
as per your comment
I need search(newText) not to be executed on collapsing.
than set this searchView.setOnQueryTextListener(null); when you don't need

my Fragment not getting executed to onCreateOptionsMenu(Menu menu, MenuInflater inflater)

I want to implement a menu to get displayed from Fragment
public class NewsTab extends Fragment {
but the below code doesn't seem to execute.
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.newsmenu, menu);
TextView text= null;
SearchView mSearchView = (SearchView) menu.findItem(R.id.search).getActionView();
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
Log.e("search sering....:", newText.toString());
dataAdapter.getFilter().filter(newText.toString());
dataAdapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
Log.e("searched ....:", constraint.toString());
return db.fetchOrdersByName(constraint.toString());
}
});
return false;
}
});
super.onCreateOptionsMenu(menu, inflater);
}`
I've used setHasOptionsMenu(true) in the onCreate method.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.news_summary_tab, container, false);
}
#Override
public void setMenuVisibility(boolean menuVisible) {
super.setMenuVisibility(menuVisible);
if (menuVisible) {
Log.e("SetMenuVisibleFragmentA", menuVisible + " ");
}
}
I've been trying to get it work since couple of days but nothing is working. I do not know what is missing here. Any help in this regard is highly appreciated.
thanks !
Your code seems almost OK.
Try using menu.clear() in your onCreateOptionsMenu() before inflating fragment menu newsmenu.
Put some log in onCreateOptionsMenu() to know if the method is not being called.
Update onCreateOptionsMenu() as below:
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear()
inflater.inflate(R.menu.newsmenu, menu);
TextView text= null;
SearchView mSearchView = (SearchView) menu.findItem(R.id.search).getActionView();
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
Log.e("search sering....:", newText.toString());
dataAdapter.getFilter().filter(newText.toString());
dataAdapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
Log.e("searched ....:", constraint.toString());
return db.fetchOrdersByName(constraint.toString());
}
});
return false;
}
});
super.onCreateOptionsMenu(menu, inflater);
}
Hope this will help~

Unable to search using SearchView implemented in StandAlone Toolbar

I'm trying to implement SearchView in standalone Toolbar in a fragment but unable to get any result.
I followed this answer SearchView at a secondary Toolbar but no success.
Here is my implementation
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_tab_fragment3, container, false);
initViews(rootView);
initBottomToolbar(rootView);
return rootView;
}
private void initBottomToolbar(View rootView) {
mToolbarBottom = (Toolbar) rootView.findViewById(R.id.toolbar_bottom);
// Set an OnMenuItemClickListener to handle menu item clicks
mToolbarBottom.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
// Handle the menu item
Toast.makeText(getActivity(), "action_search clicked", Toast.LENGTH_SHORT).show();
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = null;
if (menuItem != null) {
Log.d("Bottom Toolbar", "menuItem != null");
searchView = (SearchView) menuItem.getActionView();
}
if (searchView != null) {
Log.d("Bottom Toolbar", "searchView != null");
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
Toast.makeText(getActivity(), query, Toast.LENGTH_SHORT).show();
Log.e("Bottom Toolbar", "Submitted: "+query);
return false;
}
#Override
public boolean onQueryTextChange(String newQuery) {
Toast.makeText(getActivity(), newQuery, Toast.LENGTH_SHORT).show();
Log.d("Bottom Toolbar", "Changed: " + newQuery);
List<ContactsBean> filteredContactsBeanList = filter(mContactsBeanList, newQuery);
mContactsAdapter.animateTo(filteredContactsBeanList);
mContactsRecyclerView.scrollToPosition(0);
return true;
}
});
}
return true;
}
});
// Inflate a menu to be displayed in the toolbar
mToolbarBottom.inflateMenu(R.menu.menu_bottom_toolbar);
}
Even, if I click SearchView, it's neither showing Toast nor showing any message in Log.
I made it work by inflating first the Menu on my toolbarBottom before binding the listeners
toolbarBottom.inflateMenu(R.menu.menu_contacts_bottom);
if(toolbarBottom.getMenu().size() > 0){
SearchManager searchManager = (SearchManager) getActivity().getSystemService(
Context.SEARCH_SERVICE);
MenuItem searchMenuItem = toolbarBottom.getMenu().getItem(toolbarBottom.getMenu().size()-2);
mSearchView = (SearchView) searchMenuItem.getActionView();
if (mSearchView != null) {
mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
}
mSearchView.setOnCloseListener(new OnCloseListener() {
#Override
public boolean onClose() {
Log.d(TAG, "onClose");
return false;
}
});
mSearchView.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
Log.d(TAG, "query: " + newText);
return false;
}
});
}

Error SearchView in ActionBar

I have a Action Bar with Search View and I have a Fragment set has Option Menu. In my MainActivity SearchView work fine but in my Fragment (I need handle event OptionMenuItem to do something), after setHasOptionMenu then SearchView can't work. Here is my code
Option Menu in MainActivity
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
// Create SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSubmitButtonEnabled(false);
searchView.setQueryHint("Search Here");
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s) {
return false;
}
#Override
public boolean onQueryTextChange(String s) {
if(s.length() > 0){
Tab1.listadaptor.getFilter().filter(s);
Tab2.listadaptor.getFilter().filter(s);
}else{
pager.setAdapter(adapter);
if(SlidingTabLayout.userState){
pager.setCurrentItem(0);
}else{
pager.setCurrentItem(1);
}
}
return true;
}
};
searchView.setOnQueryTextListener(queryTextListener);
return super.onCreateOptionsMenu(menu);
}
OptionMenuItem in Fragment
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
//Do Something
}
return super.onOptionsItemSelected(item);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}
This works!! is in Kotlin, if you need more help, say me.
override fun onCreateOptionsMenu(menu: Menu): Boolean {
super.onCreateOptionsMenu(menu)
val inflater = menuInflater
inflater.inflate(R.menu.search_activity_menu, menu)
searchView = menu.findItem(R.id.btn_search_station).actionView as
SearchView
return true
}
private fun setErrorOnSearchView(searchView: SearchView, errorMessage: String) {
val id = searchView.context
.resources
.getIdentifier("android:id/search_src_text", null, null)
val editText = searchView.find<EditText>(id)
editText.error = errorMessage
}
private fun cleanErrorOnSearchView(searchView: SearchView) {
val id = searchView.context
.resources
.getIdentifier("android:id/search_src_text", null, null)
val editText = searchView.find<EditText>(id)
editText.error = null
}

Android ActionBarSherlock, SearchView `setOnCloseListener` is not working

I am using SearchView and it is working fine but only setOnCloseListener is not working; Here is my code
import com.actionbarsherlock.widget.SearchView.OnCloseListener;
and
searchView.setOnCloseListener(new OnCloseListener() {
#Override
public boolean onClose() {
Toast.makeText(context, "close", Toast.LENGTH_SHORT).show();
return false;
}
});
**EDIT****
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//Used to put dark icons on light action bar
//Create the search view
final SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
searchView.setQueryHint("Search");
searchView.setIconifiedByDefault(true);
//search button
menu.add(Menu.NONE,Menu.NONE,1,"Search a word")
.setIcon(R.drawable.abs__ic_search_api_holo_light)
.setActionView(searchView)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
MenuItem sView = menu.findItem(1);
sView.setOnActionExpandListener(this);
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
adopter.getFilter().filter(null);
Toast.makeText(getApplicationContext(), "collapse", Toast.LENGTH_LONG).show();
return true; // Return true to collapse action view
}
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
Toast.makeText(getApplicationContext(), "Expand", Toast.LENGTH_LONG).show();
return true; // Return true to expand action view
}
Solved it by myself. Just leave setOnCloseListener it will not work, and put following code in onCreateOptionsMenu
// searchView.setOnCloseListener(new OnCloseListener() {
// #Override
// public boolean onClose() {
// adapter.getFilter().filter("");
// Toast.makeText(getBaseContext(), "on close", Toast.LENGTH_SHORT).show();
// return false;
// }
// });
MenuItem menuItem = menu.findItem(ID_OF_SEARCHVIEW);
menuItem.setOnActionExpandListener(new OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
adapter.getFilter().filter("");
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
adapter.getFilter().filter("");
return true;
}
});
I have encountered the same problem with onCloseListener not invoking for the SearchView.
Understand from the bug issue raised in 25758, and some postings I have read, to invoke onCloseListener, you need to set:
searchView.setIconifiedByDefault(true);
But for my case I wanted to have the search view opened & not iconified all the time. I manage to resolve this by adding one more line below:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search_bar, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setOnQueryTextListener(queryTextListener);
searchView.setIconifiedByDefault(true);
searchView.setIconified(false);
return true;
}
The searchView.setIconified(false) will cause the searchView to open up, despite setting the default to iconified to true in the previous line. In this way, I managed to have both a SearchView
that opens up all the time & having it invoke the onCloseListener.
I'm using this code. And it works perfectly
#Override
public void onStartSearch() {
}
#Override
public void onSearch(String search) {
}
#Override
public void onCloseSearch() {
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.your_menu, menu);
// Associate searchable configuration with the SearchView
MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem); //here I user appcompat, but you can take it just from actionbarsherlock
SearchView.SearchAutoComplete searchAutoComplete = (SearchView.SearchAutoComplete) mSearchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchAutoComplete.setTextColor(getResources().getColor(R.color.action_bar_text_color));
mSearchView.setIconifiedByDefault(true);
mSearchView.setSubmitButtonEnabled(false);
/**
* Set all of different kinds of listeners
*/
MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
menuIsOpen = false;
onCloseSearch();
return true;
}
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
menuIsOpen = true;
onStartSearch();
return true;
}
});
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String s) {
onSearch(s);
return false;
}
#Override
public boolean onQueryTextSubmit(String s) {
onSearch(s);
return true;
}
});
super.onCreateOptionsMenu(menu, inflater);
}
/**
* Function for closing search when android is less than 14
*/
public boolean onBackButton() {
if (AndroidUtils.getSdkVersion() >= 14)
return false;
if (menuIsOpen) {
menuIsOpen = false;
getActivity().supportInvalidateOptionsMenu();
onCloseSearch();
return true;
}
return false;
}
After backButton it close search, or just by selecting close from action bar.
You can use a OnActionExpandListener:
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
SearchView searchView = new SearchView(((SherlockFragmentActivity) getActivity()).getSupportActionBar().getThemedContext());
searchView.setIconifiedByDefault(true);
// ...
MenuItem menuItem = menu.add(R.string.search);
// ...
menuItem.setOnActionExpandListener(this);
}
#Override
public boolean onMenuItemActionExpand(final MenuItem item) {
mInSearchMode = true;
return true;
}
#Override
public boolean onMenuItemActionCollapse(final MenuItem item) {
mInSearchMode = false;
return true;
}

Categories

Resources