What i have::
I have a menu in actionbar it has two sub-items in it
buffet_map_cart.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_cart"
android:icon="#drawable/ic_action_camera"
android:showAsAction="ifRoom"
android:title="cart">
<menu>
<item
android:id="#+id/action_cart_buy"
android:icon="#drawable/ic_action_camera"
android:title="Sort by newest"/>
<item
android:id="#+id/action_cart_reserve"
android:icon="#drawable/button_reserve_selector"
android:title="Sort by rating"/>
</menu>
</item>
</menu>
What i am trying to do::
I want to update the text dynamically for the menu buttons
action_cart_buy and action_cart_reserve
How to find this resource from the Java code and update the text ?
You can use onPrepareOptionsMenu(Menu) for dynamic updates:
Prepare the Screen's standard options menu to be displayed. This is called right before the menu is shown, every time it is shown. You can use this method to efficiently enable/disable items or otherwise dynamically modify the contents.
You can use it like:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem buyItem = menu.findItem(R.id.action_cart_buy);
MenuItem reserveItem = menu.findItem(R.id.action_cart_reserve);
buyItem.setTitle("New buyItem String");
reserveItem.setTitle("New reserveItem String");
return super.onPrepareOptionsMenu(menu);
}
Take a look at findItem from Menu and than MenuItem, there is a setTitle.
Edit--
There is a topic called "Changing menu items at runtime" in this article which may help you as well.
You have access to the Menu object in onCreateOptionsMenu or onPrepareOptionsMenu (better).
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(R.id.action_cart_buy);
item.setTitle("Any title");
return super.onPrepareOptionsMenu(menu);
}
To make the UI update your ActionBar at the right time, you have to call:
invalidateOptionsMenu();
Related
I have menu xml file in menu->main.xml in which two item are available. I have two activities and i want to add this two item in overflow menu in first activity but in the second activity i want to add only one item in overflow menu.
menu->main.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
android:id="#+id/action_refresh"
android:title="#string/action_refresh"
app:showAsAction="never"/>
<item
android:id="#+id/action_setting"
android:title="#string/action_setting"
app:showAsAction="never"/>
</menu>
So how to do that?
you can do it in two ways:
Have separate menu XMLs for both activities. one with both items and other with only one that you require.
remove the unneeded menu item in code inside 2nd activity, by overriding onCreateOptionsMenu e.g:
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main,menu);
menu.removeItem(R.id.action_setting);
return super.onCreateOptionsMenu(menu);
}
you could do it programmatically by hiding it...For example
MenuItem refreshIcon = menu.findItem(R.id. action_refresh);
refreshIcon.setVisible(false);
Get a reference to your menu item in onCreateOptionsMenu(menu) and update show as action as below in your second activity.
MenuItem menuitem = menu.finditem(your_id);
menuitem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
You can use the onCreateOptionsMenu that you'll have to override. You could try something like that, depending on what you need to show here:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.getItem(0).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
}
about android Toolbar, how to hide default setting menu in toolbar
in the this activity i hava set a single icon menu,like this –
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:title=""
android:id="#+id/menu_add"
android:icon="#drawable/actionbar_add_icon"
android:showAsAction="ifRoom">
</item>
</menu>
it is menu/main_home.xml
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_home, menu);
return super.onCreateOptionsMenu(menu);
}
but it show a three dots menu. i want know why
and how to show sub menu icon image ,it is normal in the Actionbar, but hide in the Toolbar
If you want to empty the contents of the menu, go to the res/menu folder and delete the item tags from the menu_main.xml file. You will also need to delete references to the items in onOptionsItemSelected(MenuItem item). The menu dots will disappear if there are no items.
If you want to completely remove the three dots menu, then go to the onCreateOptions(...) method in your code and delete it, or simply remove the getMenuInflater().inflate(...); portion of it. You can safely remove the onOptionsItemSelected(MenuItem item) method as well because it will have no purpose.
The simplest way to make the menu disappear is to have the onCreateOptions(...) method return false.
this question is solved use app:showAsAction ="ifRoom" not android:showAsAction ="ifRoom"
<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:title=""
android:id="#+id/menu_button"
app:actionLayout="#layout/menu_single_button"
app:showAsAction = "always"
/></menu>
I can't show items inside the "Menu Item" when those items are already displayed on the Action bar.
This is my onCreateOptionsMenu method:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.opt_menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
This is my opt_menu_main layout:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_act_settings"
android:title="Settings"
android:icon="#drawable/ic_action_settings"
android:showAsAction="ifRoom"/>
</menu>
I'm not using support library (my target API is 17).
If I click on the button Menu it is always empty because the item is already displayed up on the ActionBar.
I've tried to add more items but when the items goes up on the ActionBar, they are not displayed inside the options menu.
I've also tried with showAsAction="ifRoom|withText"but doesn't work.
I think this behavior is correct, but is it possible to show the same item both in Menu and Action Bar at the same time?
Thanks
Try this:
app:showAsAction="always"
but don't forget to add this line at the begin of definition:
xmlns:app="http://schemas.android.com/apk/res-auto"
The Id's for menu items must be unique. The best option is to create another menu item with another Id and set its showAsAction to "never" to force it always into the overflow menu. But their is a chance if you are also displaying other primary options it might force both into the overflow in which case you will see two menu items with the same title.
For the moment I solved like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_act_settings"
android:title="Settings"
android:icon="#drawable/ic_action_settings"
android:showAsAction="always"/>
<item
android:id="#+id/menu_settings"
android:title="Settings"
android:showAsAction="never"/>
</menu>
So in the onOptionsItemSelected method :
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_add_schedule:
case R.id.menu_act_add_schedule:
//some code
break;
}
return true;
}
I don't think this is a correct solution, because on some device where there is not enough space to add this and other items up on the ActionBar,
since they are defined as showAsAction="always", they will be overlapped on the ActionBar Title, so it can create some layout problems.
The behavior that I want is to show always the items on the option menu, and show it also in the action bar if there is enough space.
This behavior can not be obtained with showAsAction="always" .
It would be great if someone could find a better solution.
Thanks anyway.
I am in great need of you as i am stuck in this issue from the last 4 days.
So,here is the thing....
I have one sherlock fragment activity from which i calling the six fragments inside viewpager.
Now i want to manipulate the sherlock action bar depends on the selected tab.
So for the first 3 tab i want to show the searchview and overflow menu icon.
For that what i have tried so far ::
I am in great need of you as i am stuck in this issue from the last 4 days.
So,here is the thing....
I have one sherlock fragment activity from which i calling the six fragments inside viewpager.
Now i want to manipulate the sherlock action bar depends on the selected tab.
So for the first 3 tab i want to show the searchview and overflow menu icon.
For that what i have tried so far ::
1) Called the setHasOptionsMenu(true); in every fragment.
2) Added the onCreateOptionsMenu() in every fragment. and
then i have achieved what i want.
Now i want that onClick of the searchview it should get expanded and collapse when pressed the back. So to achieve that i have added this in my first 3 fragment and also implement the puru's logic .(Create the onPrepareOptionMenu() in every fragment and add the onQueryTextChange(""); method).
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().supportInvalidateOptionsMenu();
setHasOptionsMenu(true);
}
Fragment Code ::
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.menu_item_search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setQueryHint("Search for Products");
searchView.setOnQueryTextListener(this);
searchView.setIconifiedByDefault(false);
}
#Override
public void onPrepareOptionsMenu(Menu menu)
{
super.onPrepareOptionsMenu(menu);
onQueryTextChange("");
}
#Override
public boolean onQueryTextSubmit(String query)
{
return true;
}
#Override
public boolean onQueryTextChange(String newText)
{
return false;
}
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_item_search"
android:icon="#drawable/abs__ic_search"
android:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="com.actionbarsherlock.widget.SearchView"
android:title="Search Products">
</item>
<item
android:id="#+id/root_menu"
android:icon="#drawable/abs__ic_menu_moreoverflow_normal_holo_light"
android:showAsAction="always"
android:title="More">
<menu>
<item
android:id="#+id/menu_Home"
android:icon="#drawable/home"
android:showAsAction="never"
android:title="Home"/>
<item
android:id="#+id/menu_favourite"
android:icon="#drawable/favourite"
android:showAsAction="never"
android:title="Favourite"/>
<item
android:id="#+id/menu_Balance"
android:icon="#drawable/balance"
android:showAsAction="never"
android:title="Balance"/>
<item
android:id="#+id/menu_logout"
android:icon="#drawable/btn_logout"
android:showAsAction="never"
android:title="Logout"/>
</menu>
</item>
</menu>
Problem :: (on Pre-HoneyComb Device)
Now when i click of the first fragment's searchview,it not get expanded,but works well for fragment 2 and fragment 3.
Steps ::
1)go to directly fragment 1,click on searchview,not expanding
2)go to directly on fragment 2 tab(not clicking onsearchview in second fragment),back to fragment 1,searchview expanding
3)go to directly on fragment 3 tab(not clicking onsearchview in third fragment),back to fragment 1,searchview expanding
Is there any issue related to the focus of searchview? or am i calling the supportInvalidateOptionsMenu() in some unusual way?
Let me know if you need anything more from my side...
Please guide me in my implementation... Any clue why this strange behaviour happens? Any suggestion/explnation will be appreciated....
Plz plz help me on this issue....
Thanks in Advance...
I need to change the background of menu items only. When I tried with actionBarItemBackground, it changes the background of application logo [As u can see in the attachment] also. Any help or link will be appreciated.
You can use a custom layout for your actionbar item like so:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#id/item_text"
android:showAsAction="always"
android:actionLayout="#layout/action_text"/>
</menu>
This is styleable as you wish. Another possibility would be adding only the ID (item_text in the example) and set the background like so:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(R.id.item_text);
item.getActionView().setBackgroundDrawable(new ColorDrawable(/* your color */));
return super.onPrepareOptionsMenu(menu);
}