Changing menu items in action bar sherlock dynamically

actionbarsherlock, android, android-actionbar, menu, shopping-cart

Solution

I came up with the answer that addresses my question, I designed a customized layout for the CART Menu Item:

badge.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="48dp"
    android:layout_height="fill_parent"
    android:clickable="true"
    style="@android:style/Widget.ActionButton" >

    <!-- Menu Item Image -->
    <ImageView
        android:id="@+id/img_view"
        android:layout_width="48dp"
        android:layout_height="fill_parent"
        android:clickable="true"
        android:background="@drawable/ic_nav_cart" />

    <!-- Badge Count -->

    <TextView
        android:id="@+id/actionbar_notifcation_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="99"
        android:textColor="#9F2828"
        android:textSize="14sp"
        android:layout_toRightOf="@+id/img_view"
        android:background="@drawable/cart_circle"/>

</RelativeLayout>

In my menu.xml I include it in this way:

<item
        android:id="@+id/cart"
        android:actionLayout="@layout/badge"
        android:icon="@drawable/ic_nav_cart"
        android:showAsAction="always"
        android:title="@string/cart"/>

Then in my activity I use it like this:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.aiwmain, menu);

        //getting the count of cart items
        DBManager db = DBManager.getSingletonInstance();
        int cartItemCount = db.getCartItemCount(this);

        badgeLayout = (RelativeLayout) menu.findItem(R.id.cart).getActionView();
        menu.getItem(1).getActionView().setOnClickListener(this);
        TextView tv = (TextView) badgeLayout.findViewById(R.id.actionbar_notifcation_textview);
        tv.setText(""+cartItemCount);
        //do not show the count if count is 0
        if(cartItemCount == 0){
            tv.setVisibility(View.GONE);
        }
        return super.onCreateOptionsMenu(menu);
    }

The above method is in the BaseActivity from which all other Activities inherit. Therefore you need not include this in all activities.

To handle the onClick of the the CART ICON, do this in all activities:

public void onClick(View v) {
    // navigating to the cart details by checking the ids of menu item and
    // clicked layout.
    if (v.getId() == R.id.cart) {
        Intent order = new Intent(getApplicationContext(), CartDetailsActivity.class);
        startActivity(order);
    }
}

Reference : How to get text on an ActionBar Icon?

Problem

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. Below is a screenshot: Menu.xml: ``` <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/scanner" android:icon="@drawable/ic_nav_scanner" android:showAsAction="always" android:title="@string/scanner"/> <item android:id="@+id/cart" android:icon="@drawable/ic_nav_cart" android:showAsAction="always" android:title="@string/cart"/> <item android:id="@+id/a_More" android:icon="@drawable/ic_nav_more" android:showAsAction="always" android:title="@string/more"> <menu> <item android:id="@+id/location" android:icon="@drawable/ic_nav_dropdown_location" android:showAsAction="always" android:title="@string/location_action"/> <item android:id="@+id/fav" android:icon="@drawable/ic_nav_dropdown_favourite" android:showAsAction="always" android:title="@string/favourites"/> <item android:id="@+id/info" android:icon="@drawable/ic_nav_dropdown_information" android:title="@string/information"/> </menu> </item> </menu> ``` 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; } ``` Any help to change the cart icon on action bar dynamically is appreciated. //EDIT I managed to come up with this bit: ``` @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 > 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); } ```

Original source