Home > Blockchain >  Cart badge with bottom navigation view android
Cart badge with bottom navigation view android

Time:01-09

I'm building a shop android app which I need to set these items in bottom navigation view (Home, cart, favorites, more) and I need to set badge on cart to view how many items in cart I build bottom nav bar successfully but I can't add cart badge on icon. this is my code in activity which I set menu for nav view.
MainActivity.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_products);
        Objects.requireNonNull(getSupportActionBar()).hide();

        context = this;

        tools = Tools.getInstance(context);
        prefs = Prefs.getInstance(context);
        tools.DisplayLang(prefs.GetLang(),this);

        nav = findViewById(R.id.bottom_nav_view);
        nav.inflateMenu(R.menu.menu);
        nav.setSelectedItemId(R.id.home);
        getSupportFragmentManager().beginTransaction().replace(R.id.frag_container,new Home())
                .commit();
        nav.setOnItemSelectedListener(btm_nav);
    }

    NavigationBarView.OnItemSelectedListener btm_nav =
            new NavigationBarView.OnItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    Fragment fragment;
                    switch (item.getItemId()) {
                        case R.id.home:
                            fragment = new Home();
                            getSupportFragmentManager().beginTransaction().replace(R.id.frag_container,fragment).commit();
                            break;
                        case R.id.fav:
                            fragment = new Favourite();
                            getSupportFragmentManager().beginTransaction().replace(R.id.frag_container,fragment).commit();
                            break;
                        case R.id.cart:
                            fragment = new Cart();
                            getSupportFragmentManager().beginTransaction().replace(R.id.frag_container,fragment).commit();
                            break;
                        case R.id.more:
                            PopupMenu menu = new PopupMenu(Products.this,
                                    findViewById(R.id.more));
                            menu.getMenuInflater().inflate(R.menu.pop_up,menu.getMenu());
                            tools.setForceShowIcon(menu);
                            menu.show();
                            break;
                    }
                    return true;
                }
            };

@menu/menu

<?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/home"
        android:title="@string/home"
        android:icon="@drawable/home"/>

    <item android:id="@ id/cart"
        android:title="@string/basket"
        android:icon="@drawable/cart"
        app:actionLayout="@layout/cart_layout"/>

    <item android:id="@ id/fav"
        android:title="@string/fav"
        android:icon="@drawable/fav"/>

    <item android:id="@ id/more"
        android:title="@string/more"
        android:icon="@drawable/more"/>
</menu>

@layout/cart_layout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@ id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:src="@drawable/cart"
        app:layout_constraintEnd_toStartOf="@ id/txt_count"
        app:layout_constraintHorizontal_bias="0.458"
        app:layout_constraintStart_toStartOf="@ id/txt_count"
        app:layout_constraintTop_toTopOf="@ id/txt_count" />

    <TextView
        android:id="@ id/txt_count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="668dp"
        android:background="@drawable/cart_badge"
        android:gravity="center"
        android:padding="3dp"
        android:text="0"
        android:textColor="@color/white"
        android:textSize="10dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.598"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

I don't have any errors but my problem is that badge doesn't appear on cart icon. How can I do that ?

CodePudding user response:

If you are using the Material Components library, then there is no need to create an extra layout for your badge, unless you want a custom badge. For adding badge to a menu item, first initialize a BadgeDrawable with your menu item's id. For example:

BadgeDrawable badge = nav.getOrCreateBadge(R.id.cart);

Here, nav is your bottom navigation view and R.id.cart is your menu item's id. After initialization, set the badge to be visible by badge.setVisible(true); and finally set the number on the badge by badge.setNumber(1);. You can even set the badge's background color by badge.setBackgroundColor(getResources().getColor(R.color.red)); and badge's text color by badge.setTextColor(getResources().getColor(R.color.white));. And finally when you want to remove the number just call badge.clearNumber();.

  •  Tags:  
  • Related