Note that there are some explanatory texts on larger screens.

plurals
  1. POChanging menu items in action bar sherlock dynamically
    primarykey
    data
    text
    <p>I have implemented action bar sherlock in my android application. This application has a Shopping Cart functionality. There is a icon on the action bar that takes the user to the cart. I need to dynamically change that icon when items are added to the cart and removed from the cart. That is a number should appear on top of the cart icon in the action bar.</p> <p>Below is a screenshot:</p> <p><img src="https://i.stack.imgur.com/sIRJ0.png" alt="enter image description here"></p> <p>Menu.xml:</p> <pre><code>&lt;menu xmlns:android="http://schemas.android.com/apk/res/android" &gt; &lt;item android:id="@+id/scanner" android:icon="@drawable/ic_nav_scanner" android:showAsAction="always" android:title="@string/scanner"/&gt; &lt;item android:id="@+id/cart" android:icon="@drawable/ic_nav_cart" android:showAsAction="always" android:title="@string/cart"/&gt; &lt;item android:id="@+id/a_More" android:icon="@drawable/ic_nav_more" android:showAsAction="always" android:title="@string/more"&gt; &lt;menu&gt; &lt;item android:id="@+id/location" android:icon="@drawable/ic_nav_dropdown_location" android:showAsAction="always" android:title="@string/location_action"/&gt; &lt;item android:id="@+id/fav" android:icon="@drawable/ic_nav_dropdown_favourite" android:showAsAction="always" android:title="@string/favourites"/&gt; &lt;item android:id="@+id/info" android:icon="@drawable/ic_nav_dropdown_information" android:title="@string/information"/&gt; &lt;/menu&gt; &lt;/item&gt; &lt;/menu&gt; </code></pre> <p>Code:</p> <pre><code>@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getSupportMenuInflater(); inflater.inflate(R.menu.aiwmain, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { super.onOptionsItemSelected(item); switch (item.getItemId()) { case R.id.scanner: //scanner clicked break; } </code></pre> <p>Any help to change the cart icon on action bar dynamically is appreciated.</p> <p>//EDIT</p> <p>I managed to come up with this bit:</p> <pre><code>@Override public boolean onPrepareOptionsMenu(Menu menu) { DBManager db = DBManager.getSingletonInstance(); int cartItemCount = db.getCartItemCount(this); MenuItem cart = menu.findItem(R.id.cart); MenuItem scanner = menu.findItem(R.id.scanner); if(cartItemCount &gt; 0){ //I want this item to be changed, I do not want to have seperate drawables for all numbers. cart.setIcon(R.drawable.arrow_left); } return super.onPrepareOptionsMenu(menu); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload