I'm inflating a menu with menu items in Activity's toolbar and overwriting the options in fragment.But the new menu items are not showing up.
Activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.encounter_tabacivity_menu, menu);
return true;
}
Fragment
#Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.clear(); //remove all items
getActivity().getMenuInflater().inflate(R.menu.encounter_frag_problemlist_menu, menu);
}
#Override
public void onResume() {
super.onResume();
getActivity().invalidateOptionsMenu();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
view = inflater.inflate(R.layout.fragment_problem_list, container, false);
init();
return view;
}
You might not have set your toolbar as your actionbar. To view your menu in your actionbar declare your toolbar as actionbar as follows
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
You need setHasOptionMenu(true) in your fragment.
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
You must override this in fragment
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Add your menu entries here
super.onCreateOptionsMenu(menu, inflater);
}
Try this code in your Fragment :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_record ) {
// startActivity(new Intent(getActivity(),RecordingActivity.class));
startActivity(new Intent(getActivity(),RecordingTestActivity.class));
getActivity().finish();
return true;
}
if (id == R.id.action_search ) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
inflater.inflate(R.menu.recording, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
if (searchItem != null) {
searchView = (SearchView) searchItem.getActionView();
}
if (searchView != null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
queryTextListener = new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) {
Log.i("onQueryTextChange", newText);
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
Log.i("onQueryTextSubmit", query);
return true;
}
};
searchView.setOnQueryTextListener(queryTextListener);
} super.onCreateOptionsMenu(menu, inflater);
}
Related
I am trying to hide/show menuItem but getting NPE. It works in onPrepareOptionsMenu but not in onCreate. By default it should be invisible.
e.g. in my fragment's onCreate i would like to do something like:
if (condition) menuItem.setvisible(true);
but it gets me NPE on that menuItem.
Activity
#Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.inventory_list_toolbar, menu);
return true;
}
#Override
protected void onCreate(final Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_inventory);
mToolbar = findViewById(R.id.inventory_list_toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
Fragment:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public void onPrepareOptionsMenu(Menu menu){
super.onPrepareOptionsMenu(menu);
menuItem = menu.findItem(R.id.inventory_list_menu_add);
menuItem.setVisible(false);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
You can't hide menu from onCreate method since it's not inflated yet.
You should make your logic in onPrepareOptionsMenu method:
#Override
public void onPrepareOptionsMenu(Menu menu) {
if (condition) {
// do what you want
MenuItem menuItem = menu.findItem(R.id.menu_item_id);
menuItem.setvisible(true);
}
return super.onPrepareOptionsMenu(menu);
}
Otherwise, put your logic in onCreateOptionsMenu method and call invalidateOptionsMenu() when you need to refresh something.
private Menu menu;
Then initialize it as
#Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present.
this.menu = menu;
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
When u want to hide/show the menu use
if(menu != null){
MenuItem item_up = menu.findItem(R.id.action_up);
item_up.setVisible(true);
}
My toolbar contains search icon and refresh icon,when i click on search icon then search view inflate from onCreateOptionMenu() but the refresh icon does not get hide.If i hide refresh icon in onMenuItemActionExpand() then on back press of search view back icon both menu items search and refresh gets hide and default setting icon gets visible.Below is the code which i have implemented.Please help me!!!
MenuItem.java
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
item_search = menu.findItem(R.id.action_search);
item_refresh = menu.findItem(R.id.action_refresh);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item_search);
searchView.setOnSearchClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// hide action item
if (menu != null) {
menu.findItem(R.id.action_refresh).setVisible(false);
}
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
#Override
public boolean onClose() {
// re-show the action button
if (menu != null) {
menu.findItem(R.id.action_refresh).setVisible(true);
}
return false;
}
});
Well you could imitate that yourself by hiding all the other items when the SearchView is expanded:
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
final MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = (android.widget.SearchView) searchItem.getActionView();
// Detect SearchView icon clicks
searchView.setOnSearchClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setItemsVisibility(menu, searchItem, false);
}
});
// Detect SearchView close
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
#Override
public boolean onClose() {
setItemsVisibility(menu, searchItem, true);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
private void setItemsVisibility(Menu menu, MenuItem exception, boolean visible) {
for (int i=0; i<menu.size(); ++i) {
MenuItem item = menu.getItem(i);
if (item != exception) item.setVisible(visible);
}
}
MenuItem action_search;
MenuItem action_refresh;
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
action_search = menu.findItem(R.id. action_search);
action_refresh = menu.findItem(R.id. action_refresh);
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
item_search = menu.findItem(R.id.action_search);
item_refresh = menu.findItem(R.id.action_refresh);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item_search);
searchView.setOnSearchClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// hide action item
if (action_refresh != null) {
action_refresh.setVisible(false);
invalidateOptionsMenu();
}
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
#Override
public boolean onClose() {
// re-show the action button
if (action_search != null) {
action_search.setVisible(true);
}
if (action_refresh != null) {
action_refresh.setVisible(true);
}
invalidateOptionsMenu();
return false;
}
});
It seems that it's a bug from android, you can use the solution here!
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.
I have an Activity with DrawerLayout and a RelativeLayout (with ViewPager and a FrameLayout as children for it) and a ListView as children. I am loading a Fragment inside the FrameLayout when an item in the menu inside DrawerLayout is clicked.
The ViewPager loads 3 Fragments. Each of the Fragment has a menu with 3 items. The Fragment that is loaded inside the FrameLayout has menu with 2 items.
The problem is, when i switch between ViewPager and the FrameLayout's Fragment, the menu items are getting added up, to each other.
Please refer the screenshots below..
I am guessing, that i am doing something wrong in onCreateOptionsMenu, onOptionsItemSelected or onPrepareOptionsMenu methods.
Activity's Methods:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
if (mDrawerLayout.isDrawerOpen(mDrawerLeft)) {
mDrawerLayout.closeDrawer(mDrawerLeft);
} else {
mDrawerLayout.openDrawer(mDrawerLeft);
}
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
return super.onPrepareOptionsMenu(menu);
}
One of the ViewPager's Fragment: Other two are also similar:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_all, menu);
SearchManager searchManager = (SearchManager) activity.
getSystemService(Context.SEARCH_SERVICE);
searchMenuItem = menu.findItem(R.id.ic_action_search);
searchView = (SearchView) searchMenuItem.getActionView();
searchView.setSearchableInfo(searchManager.
getSearchableInfo(activity.getComponentName()));
searchView.setOnQueryTextListener(this);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_list) {
prefs.edit().putString(Const.VIEWPAGER.VIEW_TYPE, Const.VIEWPAGER.LIST_VIEW).apply();
mStaggeredLayoutManager.setSpanCount(1);
} else if (item.getItemId() == R.id.action_grid) {
prefs.edit().putString(Const.VIEWPAGER.VIEW_TYPE, Const.VIEWPAGER.GRID_VIEW).apply();
mStaggeredLayoutManager.setSpanCount(2);
} else if (item.getItemId() == R.id.action_mini) {
prefs.edit().putString(Const.VIEWPAGER.VIEW_TYPE, Const.VIEWPAGER.MINI_VIEW).apply();
mStaggeredLayoutManager.setSpanCount(1);
}
ActivityCompat.invalidateOptionsMenu(activity);
mViewPagerPetitionsRecyclerViewAdapter.notifyDataSetChanged();
mIMainAllPetitionsListener.onLayoutChangedListener();
return super.onOptionsItemSelected(item);
}
#Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem list_item = menu.findItem(R.id.action_list);
MenuItem grid_item = menu.findItem(R.id.action_grid);
MenuItem mini_item = menu.findItem(R.id.action_mini);
String item_type = prefs.getString(Const.VIEWPAGER.VIEW_TYPE, Const.VIEWPAGER.LIST_VIEW);
if (item_type.equalsIgnoreCase(Const.VIEWPAGER.LIST_VIEW)) {
list_item.setChecked(true);
grid_item.setChecked(false);
mini_item.setChecked(false);
} else if (item_type.equalsIgnoreCase(Const.VIEWPAGER.GRID_VIEW)) {
list_item.setChecked(false);
grid_item.setChecked(true);
mini_item.setChecked(false);
} else if (item_type.equalsIgnoreCase(Const.VIEWPAGER.MINI_VIEW)) {
list_item.setChecked(false);
grid_item.setChecked(false);
mini_item.setChecked(true);
}
}
FrameLayout's Fragment:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_id_card, menu);
MenuItem menuItem = menu.findItem(R.id.edit);
menuItem.setVisible(false);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_share) {
shareIDCard();
} else if (id == R.id.action_save_as_image) {
saveAsImage();
} else if (id == R.id.action_save_as_wallpaper) {
SaveAsWallPaper();
} else if (id == R.id.edit) {
setEnabled(true);
isEditable = true;
ActivityCompat.invalidateOptionsMenu(activity);
} else if (item.getItemId() == R.id.save) {
....
ActivityCompat.invalidateOptionsMenu(activity);
} else if (item.getItemId() == R.id.cancel) {
...
ActivityCompat.invalidateOptionsMenu(activity);
}
return super.onOptionsItemSelected(item);
}
#Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem menuItemEdit = menu.findItem(R.id.edit);
MenuItem menuItemSave = menu.findItem(R.id.save);
MenuItem menuItemCancel = menu.findItem(R.id.cancel);
....
menuItemEdit.setVisible(false);
menuItemSave.setVisible(false);
menuItemCancel.setVisible(false);
}
Can someone help me understand why is this happening and what can i do to fix this?
Add menu.clear(); in your onCreateOptionsMenu before inflate
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()
}