Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make BadgeCount on the menuItem on toolbar

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 406
    Comment on it

    If you are looking to create BadgeCount view in android toolbar, here is the way to code it:-

    Step 1:- Create badgeCount layout file say badgecount.xml :-

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/cart_icon"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_marginTop="10dp"
            android:clickable="true"
            android:src="@drawable/cart" />
        <!-- Badge Count -->
    
        <TextView
            android:id="@+id/count_badge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/cart_icon"
            android:text="0"
            android:textColor="@android:color/holo_orange_dark" />
    
    </RelativeLayout>
    

    Step 2:- Include above badgecount.xml in the menu.xml using "actionLayout" attribute of item:-

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:com.badgedemo="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          tools:context=".MainActivity">
        <item android:id="@+id/cart_holder"
            com.badgedemo:actionLayout="@layout/batch_icon"
            android:title=""
            com.badgedemo:showAsAction="always" />
        <item android:id="@+id/menu_search"
            android:icon="@drawable/search"
            android:title="Setting"
            com.badgedemo:showAsAction="always"/>
    </menu>
    

    Step 3: Code snippet in main activity:-

    @Override
    protected void onCreate(Bundle savedInstanceState) 
       {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity&amp;#95;main);
        Toolbar tb = (Toolbar) findViewById(R.id.toolbar);
        ImageView homeIcon = (ImageView) tb.findViewById(R.id.toolbar&amp;#95;home);
        homeIcon.setOnClickListener(new View.OnClickListener() 
          {
            @Override
              public void onClick(View view)
               {
                 cartCount++;
                 invalidateOptionsMenu();
               }
          });
          setSupportActionBar(tb);
       }
    
    
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
      {
        Toolbar tb = (Toolbar) findViewById(R.id.toolbar);
         tb.inflateMenu(R.menu.menu_main);
         MenuItem itemCart = menu.findItem(R.id.cart_holder);
         MenuItem itemSetting = menu.findItem(R.id.menu_setting);
         TextView count=(TextView)menu.findItem(R.id.cart_holder).getActionView().findViewById(R.id.count_badge);
        count.setText("" +cartCount);
        tb.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener()
            {
             @Override
             public boolean onMenuItemClick(MenuItem item)
                {
                   return onOptionsItemSelected(item);
                }
             });
          return true;
         }
    
    
    
    @Override
     public boolean onOptionsItemSelected(MenuItem item)
        {
          switch (item.getItemId()) 
            {
             case R.id.cart_holder:
               Toast.makeText(this, "Value at your cart is"+cartCount, Toast.LENGTH_SHORT) .show();
                break;
             case R.id.menu_setting:
               Toast.makeText(this, "Settings selected", Toast.LENGTH_SHORT).show();
                break;
             default:
                break;
            } 
         return true;
        }
      }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: