Notification Badge On Action Item Android

后端 未结 6 1085
感动是毒
感动是毒 2020-12-04 06:06

I wana add a notification badge on the cart image placed in action bar and manipulate it programmatically. Any Help?

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-04 06:23

    You can show custom MenuItem on ActionBar by creating a custom layout for MenuItem. To set a custom layout you have to use menu item attribute app:actionLayout.

    Follow below steps to create a Badge on Cart action item. See the attached image for result.

    1. Create a custom layout with ImageView(for cart icon) and TextView(for count value)

    layout/custom_action_item_layout.xml:

    
    
    
        
    
        
    
    
    
    1. Create drawable circular badge background using Shape.

    drawable/badge_background.xml:

    
    
    
        
        
    
    
    
    1. Add custom layout to menu item.

    menu/main_menu.xml

    
    
        
    
    
    
    1. In your MainActivity, add following codes:

    MainActivity.java:

    public class MainActivity extends AppCompatActivity {
        ................
        ......................
        TextView textCartItemCount;
        int mCartItemCount = 10;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            .....................
            ............................
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.main_menu, menu);
    
            final MenuItem menuItem = menu.findItem(R.id.action_cart);
    
            View actionView = menuItem.getActionView();
            textCartItemCount = (TextView) actionView.findViewById(R.id.cart_badge);
    
            setupBadge();
    
            actionView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onOptionsItemSelected(menuItem);
                }
            });
    
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
    
            switch (item.getItemId()) {
    
                case R.id.action_cart: {
                    // Do something
                    return true;
                }
            }
            return super.onOptionsItemSelected(item);
        }
    
        private void setupBadge() {
    
            if (textCartItemCount != null) {
                if (mCartItemCount == 0) {
                    if (textCartItemCount.getVisibility() != View.GONE) {
                        textCartItemCount.setVisibility(View.GONE);
                    }
                } else {
                    textCartItemCount.setText(String.valueOf(Math.min(mCartItemCount, 99)));
                    if (textCartItemCount.getVisibility() != View.VISIBLE) {
                        textCartItemCount.setVisibility(View.VISIBLE);
                    }
                }
            }
        }
    
        ..................
        ..............................
    
    }
    

    OUTPUT:

提交回复
热议问题