I need to change the title of a menu item in my action bar at startup based on a few variables which get created at the startup.
But for some reason I cant simply do that since the menu items take time to inflate maybe?
how do I get around this issue.
below is my attempt but it throws java.lang.IndexOutOfBoundsException
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.map_fragment_action_menu, menu);
mOptionsMenu = menu;
mOptionsMenu.getItem(R.id.map_fragment_action_layers_0).setTitle("my title");
}
P.S. I am using a fragment, I also tried to set the title in onCreateView() method but still doesn't work.
You need to use the method Menu#findItem() instead.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.map_fragment_action_menu, menu);
mOptionsMenu = menu;
mOptionsMenu.findItem(R.id.map_fragment_action_layers_0).setTitle("my title");
}
Menu#getItem() expects an index and not the menu item's id. For e.g, if this menu item is the first item in the menu, you would use
mOptionsMenu.getItem(0).setTitle("my title");
Related
I had an inflater menu created in a fragment.
This is the code.
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
It works, but I want that if I click on a button the inflater is disable. It means that also if I click on the inflater it doesn't show the menu. When I click on another button the inflater return to show its options. How can I do? Thanks!
I have a fragment extends android.support.v4.app.Fragment. And I need a checkbox in my menu, but I know I can't use getSupportActionBar(), so How another choice I have?
Thanks a lot.
in your onCreateView add this line:
setHasOptionsMenu(true);
then override the following :
#Override
public void onCreateOptionsMenu(final Menu menu, MenuInflater inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
inflater.inflate(R.menu.menu_frag, menu);
}
I am trying to update my badge based on click events of buttons inside a recycler view. this is my code:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.navigation_drawer, menu);
item = menu.findItem(R.id.add);
badgeLayout = (RelativeLayout) MenuItemCompat.getActionView(item);
mCounter = (TextView) badgeLayout.findViewById(R.id.counter);
cartButton=(Button) badgeLayout.findViewById(R.id.button1);
cartButton.setTypeface(icon);
super.onCreateOptionsMenu(menu, inflater);
}
I want to update the value of mCounter which here:
This is the recyclerview code:
holder.cartBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCounter.setText("2");// THIS DOES NOT WORK
}
});
PLEASE HELP ME!
You can perform this task in 4 ways :
Create public static variable and set its value to your badge TextView.
create BroadcastReceiver and call it when value changes.
Use EventBus reference
Create an interface
You should rather pass this event (or just the number You want to set) back to activity, invoke invalidateOptionsMenu(); and in the function onPrepareOptionsMenu(Menu menu) invoke the code You have in onCreateOptionsMenu(Menu menu, MenuInflater inflater)
I’m trying to add a SearchView in the ActionBar of a Fragment which is a part of DrawerLayout. I need the SearchView only for one fragment. I do the following in the onCreateOptionsMenu() method of the Fragment.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
menu.clear();
inflater.inflate(R.menu.alarm_list_menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
}
mSearchView is null every time. Have I missed some thing? I ‘m able to see the search icon on the ActionBar, but nothing more can be done.
So I have a Search Button in my Action Bar, But I only want that button in the first Activity, but when I open the seconde activity, the Action button is still there. How to get rid of that button in the seconde activity without removing the action bar, because I nee it from my Navigation Drawer.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
yourapp:showAsAction="ifRoom" />
...
</menu>
Activity1:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
Activity2:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//I thought removing the INFLATER part would help, but it didn't...
return super.onCreateOptionsMenu(menu);
}
You can try hiding the search menu item in your second activity.
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.findItem(R.id.action_search).setVisible(false);
return true;
}
You can refer to this question with multiple answers.
You can make the Item in your 2nd Activity invisible like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(R.id.action_search);
item.setVisible(false);
return super.onCreateOptionsMenu(menu);
}
Another approach would be to load a second menu.xml with no item in it.
if you return false it will not be shown. as android docment saying http://developer.android.com/reference/android/app/Activity.html#onCreateOptionsMenu%28android.view.Menu%29
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return false;
}