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

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~

Related

Adding a progressbar inside searchview 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.

SearchView getting reset when moved from one fragment to other in viewpager android

I have been working on an application where I have 6 tabs each of them having a listview in it. I want to filter the listview in each fragment depending upon the text in SearchView which is part of MainActivity. I have a working code for filtering records written in Listview adapter.
The issue is, whenever I want to search something using searchview, the current fragment listview gets filtered but as soon as I move from one fragment to other, the searchview gets reset and the text written in gets clear. That's why I don't get anything in other fragment to search for.
I have following code logic used
In MainActivity :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_order, menu);
if(searchView == null)
searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
return false;
}
In each of the fragment I have :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_items, container, false);
setHasOptionsMenu(true);
return view;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public void onPrepareOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onPrepareOptionsMenu(menu);
android.view.MenuItem item = menu.findItem(R.id.action_search);
SearchView sv = new SearchView(((OrderActivity) getActivity()).getSupportActionBar().getThemedContext());
MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
MenuItemCompat.setActionView(item, sv);
sv.setOnQueryTextListener(this);
sv.setIconifiedByDefault(false);
}
#Override
public boolean onQueryTextSubmit(String query) {
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
if (listAdapter != null) {
listAdapter.filter(newText);
}
return true;
}
Please suggest for solution on above issue.
Thanks in advance.

SearchView is not hiding from Toolbar in different Fragments

I have implemented SearchView inside one of a Fragment of Tablayout. It is working fine(show/hide) when switching between tablayout fragments.
My issue is that I have a HomeActivity in which I have implemented NavigationDrawer in one of a Fragment I have added TabLayout and in one of TabLayout Fragment I have added SearchView. Issue occurs when I am switching from Fragment which has a SearchView to Fragment from NavDrawer.
Searchview remains up there in other fragments too and when I switch back to that fragment in which I have added searchView then that fragment add one more searchview over Toolbar.
Here's my code from Fragment which as a SearchView
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
setHasOptionsMenu(true);
....
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_search, menu);
LogUtils.LOGI("menu added", "menu");
// Associate searchable configuration with the SearchView
//SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
//SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
//searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
final MenuItem item = menu.findItem(R.id.search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String query) {
LogUtils.LOGI("Search filter", query);
final ArrayList<EventModel> filteredModelList = filter(allEventseventsArrayList, query);
allEventsListAdapter.animateTo(filteredModelList);
eventbriteListRecyclerView.scrollToPosition(0);
return true;
}
});
searchView.setOnSearchClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LogUtils.LOGI("Search", "Search Expands");
titleIconImageView.setVisibility(View.GONE);
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
#Override
public boolean onClose() {
LogUtils.LOGI("Search", "Search Collapse");
titleIconImageView.setVisibility(View.VISIBLE);
return false;
}
});
MenuItemCompat.setOnActionExpandListener(item,
new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
// Return true to allow the action view to expand
titleIconImageView.setVisibility(View.GONE);
LogUtils.LOGI("Menu", "Menu Expands");
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
// When the action view is collapsed, reset the query
LogUtils.LOGI("Menu", "Menu Collapse");
titleIconImageView.setVisibility(View.VISIBLE);
// Return true to allow the action view to collapse
return true;
}
});
super.onCreateOptionsMenu(menu, inflater);
}
Fragment code in which I don't want SearchView
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
setHasOptionsMenu(false);
....
}
I haven't added any code related to menu in activity.
First run Image with searchView
Image after switching to different fragment from navMenu
Image again switching to fragment which has menu
It's a bit hacky but I solved it by setting setHasOptionsMenu to true and clearing the Menu in onCreateOptionsMenu via
public void onCreateOptionsMenu (Menu menu, MenuInflater inflater){
menu.clear()
}

Android:Text in searchView disappears on Configuration Change

The text i enter in searchView disappears on config change.
I am already handling the config change but still its not working.
As i have used fragments, so below code is written inside fragment.
Please solve my problem with respect to that.
My problem is i am not able to retrieve savedInstance in onActivityCreated method
Below is my code:
Snippets of useful code
private String searchQuery="";
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if(savedInstanceState != null)
searchQuery = savedInstanceState.getString(Tag.SEARCH_QUERY);
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(Tag.SEARCH_QUERY, searchQuery);
}
#Override
public void onCreateOptionsMenu(Menu menu,MenuInflater menuInflater) {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.menu_search, menu);
MenuItem menuItem = menu.findItem(R.id.action_search);
SearchView searchView = null;
if (menuItem != null)
searchView = (SearchView) menuItem.getActionView();
if (searchView != null) {
searchView.setQuery(searchQuery,false);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
//searching is done in async task
searchQuery = query;
NetworkUtility.onProgressBarShow(getActivity());
MyAsyncTaskDownloadDetails myAsyncTaskDownloadDetails = new MyAsyncTaskDownloadDetails();
myAsyncTaskDownloadDetails.execute(new String[]{Tag.PLP_URL + query, Tag.PLP,""});
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
return true;
}
});
}
super.onCreateOptionsMenu(menu, menuInflater);
}
}
First of all you are saving queryText in onQueryTextSubmit callback which means searchQuerywill remain empty unless and until user have submitted query for search, so keep that in mind.
Secondly try following code. It will work I have tested it.
if(searchQuery.length()>0){
//See this commented code
// if(Utils.hasIceCreamSandwich()) {
// searchMenuItem.expandActionView();
// }else {
// MenuItemCompat.expandActionView(searchMenuItem);
// }
MenuItemCompat.expandActionView(menuItem);
searchView.setQuery(searchQuery, false);
}

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