I've created a share button on my Action Bar - but it seems to appear twice.
The menu XML file is below:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_share"
android:title="#string/action_share"
app:showAsAction="always"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
/>
</menu>
And it is instantiated in the onCreateOptionsMenu in the view.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_detail, menu);
MenuItem menuItem = menu.findItem(R.id.action_share);
mShareActionProvider =
(ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
if(mShareActionProvider != null && !mForecastString.isEmpty()){
mShareActionProvider.setShareIntent(createShareForecastIntent());
} else{
Log.d(LOG_TAG, "Share Action provider is null?");
}
super.onCreateOptionsMenu(menu,inflater);
}
How could the share button appear twice if it is defined, inflated, and instantiated only once?
You are inflating Menu twice, both in the Activity and the Fragment.
Removing one inflation should fix the problem.
Just before inflating menu options use menu.clear();
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.menu_detail, menu);
MenuItem menuItem = menu.findItem(R.id.action_share);
mShareActionProvider =
(ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
if(mShareActionProvider != null && !mForecastString.isEmpty()){
mShareActionProvider.setShareIntent(createShareForecastIntent());
} else{
Log.d(LOG_TAG, "Share Action provider is null?");
}
super.onCreateOptionsMenu(menu,inflater);
}
This is happening due to repetitive inflation of menu.
use menu.clear() before inflation.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.my_menu_layout, menu);
}
Related
I added a ShareActionProvider menu item to my add, but when I press the "Share" button, the onOptionsItemSelected function doesn't capture the event, and I don't succeed to capture the changes in the text (the shareContent is initialized with the onCreate data through the onCreateOptionsMenu function).
I found in old posts that in older versions (support.v7), the "MenuItemCompat" needed to be used, but it's not recognized in android.widget.ShareActionProvider class.
This is part of my code:
menu.xml:
<item
android:id="#+id/action_share"
android:title="#string/action_title_share"
android:actionProviderClass="android.widget.ShareActionProvider"
android:showAsAction="always" />
mainActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem shareItem = menu.findItem(R.id.action_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) shareItem.getActionProvider();
setShareIntent(createShareIntent());
return true;
}
#Override
public boolean onOptionsItemSelected (MenuItem item)
{
switch (item.getItemId() )
{
case R.id.action_share:
//share note content
noButtonWasPressed = false;
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
setShareIntent(createShareIntent());
return true;
Any suggestions?
I want know if I can change the menu item icon after the menu it's initialized, this is my code:
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.right_menu, menu);
this.menu = menu;
updateMenuButton();
}
public void updateMenuButton() {
if (menu != null) {
if (verificato) {
this.menu.getItem(R.id.action_home).setIcon(R.drawable.ic_clear_white_24dp);
} else {
this.menu.getItem(R.id.action_home).setIcon(R.drawable.ic_done_white_24dp);
}
}
}
I call updateMenuButton at the end of onCreateOptionsMenu, but when try to access to this:
this.menu.getItem(R.id.action_home).setIcon(R.drawable.ic_clear_white_24dp);
I get this error:
java.lang.IndexOutOfBoundsException: Invalid index 2131624115, size is 1
so I think that the item menu is not added yet? if i remove the updateMenuButton() call at the end of onCreateOptionsMenu I can see the Menu Item.
How can I do?
Thanks
That because "getItem(int index)" gets the menu item at the given index.
I think you have to write menu.getItem(0);
Hi this is my first question. i am learning android. here i am trying to setup menu icon top menubar.
I have added sets of item in menu. i want to manage icon from activity.
I am trying to show hide menu icon.
Menu return null in onCreate.
Is there any other way to manage menu icon dynamically ?
please help.
This is the activity class code snippet where i am trying to manage menu.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mMenu = (Menu) findViewById(R.id.menuBar);//here Menu return null
mMenuItem = mMenu.getItem(2);
mMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
mMenuItem.setVisible(true);
}
will appreciate your help. thanks.
First inflate menu to get MenuItem in activity method onCreateOptionsMenu and then try to get menu.getItemlike this :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_activity, menu);
MenuItem item=menu.getItem(2);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
item.setVisible(true);
return true;
}
Dont put it on your onCreate because your menu is initialized in onCreateOptionsMenu
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.your_menu, menu);
mMenuItem = menu.getItem(2);
mMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
mMenuItem.setVisible(true);
return true;
}
Just follow the bellow instruction:
Step 1:
You need to inflate menu item from onCreateOptionsMenu(Menu menu)
Step 2:
You need a MenuInflater object that you can get using getMenuInflater()
API. Like: MenuInflater inflater = getMenuInflater();
Step 3:
Inflate you menu xml file like: inflater.inflate(R.menu.menu_bottom_nav,
menu);
Step 4:
You have to get menu object for specific item like : MenuItem menuItem =
menu.getItem(index). Here Index is the number depending on which menu item's
object you want to get.
Full code example:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_bottom_nav, menu);
MenuItem menuItem = menu.getItem(0);
return true;
}
action bar (Toolbar) on android has 3 options menu. Its working fine. But the problem is when ever i am uninstalling and reinstalling this duplication of the menu item is showing. Also the Title is also remains same, not changing. after sometimes, say if we close the app and restarts its working fine. I am totally confused and struck.
Here is the Code, MainActivity (OnCreateOptionsMenu):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.clear();
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_main, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) MainActivity.this.getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = null;
if (searchItem != null) {
searchView = (SearchView) searchItem.getActionView();
}
if (searchView != null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(MainActivity.this.getComponentName()));
}
MenuItem item = menu.findItem(R.id.action_wishlist);
wishlistCount = (RelativeLayout) MenuItemCompat.getActionView(item);
wishlistCount.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
setWishlistCount();
MenuItem cartitem = menu.findItem(R.id.action_cart);
cartCount = (RelativeLayout) MenuItemCompat.getActionView(cartitem);
cartCount.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
restoreToolbar();
return super.onCreateOptionsMenu(menu);
}
Here is my menu_main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<item
android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
<item
android:id="#+id/action_wishlist"
android:orderInCategory="300"
android:title="#string/wishlist"
app:actionLayout="#layout/actionbar_badge_layout"
android:icon="#drawable/ic_wishlist_dark"
app:showAsAction="always"></item>
<item
android:id="#+id/action_cart"
android:orderInCategory="300"
android:title="#string/cart"
app:actionLayout="#layout/actionbar_badge_cart_layout"
android:icon="#drawable/ic_cart_dark"
app:showAsAction="always"></item>
</menu>
Here is the Screenshot:
1: first time:
enter image description here
2: normal:
enter image description here
Note: The problem occurs when the application opens with opening navigation bar, else its working fine. On createoptionsmenu is called at all times, but still its not working. Anyone can help on this?
More Precisely this should do the trick:
menu.clear() and return super.onCreateOptionsMenu(menu); does the trick! :)
#Override
public boolean onPrepareOptionsMenu(final Menu menu)
{
menu.clear();
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return super.onCreateOptionsMenu(menu);
}
CHEERS! :)
I have cleared it by myself, by changing the code from "onCreateOptionsMenu" to "OnPrepareOptionsMenu". Also it is recommended to call Super class before clearing or inflating the menu.
Thanks for all.
I have multiple fragments in an activity where I want a search button in the toolbar. In one of my fragments this all successfully works but when I copied the exact same code into my other two fragments they aren't functioning properly.
When I click the search button on the working fragment, the keyboard pops up and I can start entering text. But on the other two fragments when I press on the search button icon, it slides to the left and then I need to press it again for it to work. Anyone know how I can fix this?
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.search, menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchView sv = new SearchView(((Home) 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(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
list.clear();
for (int i = 0; i < mainList.size(); i++) {
if (mainList.get(i).getName().toLowerCase().startsWith(newText.toLowerCase(), 0) || mainList.get(i).getAddress().toLowerCase().startsWith(newText.toLowerCase())) {
list.add(mainList.get(i));
}
}
adapter.notifyDataSetChanged();
return false;
}
});
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.action_search:
getActivity().onSearchRequested();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Try to define searchView in menu item.
<item android:id="#+id/action_search"
android:icon="#drawable/ic_search_white_24dp"
android:title="#string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|always" />
Hey i have same issue and i found solution for it. it may be because of you are giving a whole menu for search view. so when you click first time on search icon that full menu gets load and then you again click on search icon and then search view gets open.
Try below code
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
inflater.inflate(R.menu.searchview, menu);
MenuItem item = menu.findItem(R.id.menu_item_search);
SearchView sv = (SearchView) item.getActionView();
if (sv != null){
sv.setSubmitButtonEnabled(true);
sv.setOnQueryTextListener(this);
}
super.onCreateOptionsMenu(menu, inflater);
}
searchview.xml
<?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
app:showAsAction="always|collapseActionView"
android:icon="#android:drawable/ic_menu_search"
android:id="#+id/menu_item_search"
android:orderInCategory="1"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"
android:iconifiedByDefault="true"/>
</menu>
And make sure you import android.support.v7.widget.SearchView otherwise it will give error in casting.
Hope it will help.