0

I have methods which show badges on some items on my bottomNavigtionView. Here it is:

private fun addBadgeView() {
        val menuView = mainNavigationView.getChildAt(0) as BottomNavigationMenuView
        val itemView = menuView.getChildAt(1) as BottomNavigationItemView
        val notificationBadge = LayoutInflater.from(this).inflate(R.layout.notification_badge, menuView, false)
        val textView = notificationBadge.findViewById<TextView>(R.id.counter_badge)
        val itemView1 = menuView.getChildAt(2) as BottomNavigationItemView
        val notificationBadgeOne = LayoutInflater.from(this).inflate(R.layout.notification_badge, menuView, false)
        val textView1 = notificationBadgeOne.findViewById<TextView>(R.id.counter_badge)

        val itemView2 = menuView.getChildAt(3) as BottomNavigationItemView
        val notificationBadge2 = LayoutInflater.from(this).inflate(R.layout.notification_badge, menuView, false)
        val textView2 = notificationBadge2.findViewById<TextView>(R.id.counter_badge)

        when {
            sp!!.getInt("notepad_count", 0) > 99 -> textView2.text = resources.getText(R.string.more90)
            sp!!.getInt("notepad_count", 0) < 99 -> textView2.text = sp!!.getInt("notepad_count", 0).toString()
            sp!!.getInt("notepad_count", 0) == 0 -> textView2.visibility = View.GONE
        }

        if (sp!!.getString("new_receivedM", "")!!.isNotEmpty()) {
            when {
                Integer.parseInt(sp!!.getString("new_receivedM", "")!!) > 99 -> textView.text = resources.getText(R.string.more90)
                sp!!.getString("new_receivedM", "") == "0" -> textView.visibility = View.GONE
                else -> textView.text = sp!!.getString("new_receivedM", "")
            }
        } else run { textView.visibility = View.GONE }


        if (sp!!.getString("all_jobs", "")!!.isNotEmpty()) {
            when {
                Integer.parseInt(sp!!.getString("all_jobs", "")!!) > 99 -> textView1.text = resources.getText(R.string.more90)
                sp!!.getString("all_jobs", "") == "0" -> textView1.visibility = View.GONE
                else -> textView1.text = sp!!.getString("all_jobs", "")
            }
        } else {
            textView1.visibility = View.GONE
        }

        itemView.addView(notificationBadge)
        itemView1.addView(notificationBadgeOne)
        itemView2.addView(notificationBadge2)
    }

but sometimes I would like to remove some items at some conditions, so I have added here small condition:

val set = sp!!.getStringSet("disabled_app_modules", HashSet<String>())

if (set!!.isNotEmpty()) {
   val list: ArrayList<String> = ArrayList()
   list.addAll(set)
   for (i in 0 until list.size) {
    when (list[i].substring(1, list[i].length - 1)) {

      "notepad" -> {
         bottomNavigationView.menu.removeItem(R.id.notespec)
       }
     }
 }
}

and I check whether my bottomNavigationView contains this items:

if (bottomNavigationView.getChildAt(2) != null) {
            val itemView1 = menuView.getChildAt(2) as BottomNavigationItemView
            val notificationBadgeOne = LayoutInflater.from(this).inflate(R.layout.notification_badge, menuView, false)
            val textView1 = notificationBadgeOne.findViewById<TextView>(R.id.counter_badge)

            if (sp!!.getString("all_jobs", "")!!.isNotEmpty()) {
                when {
                    Integer.parseInt(sp!!.getString("all_jobs", "")!!) > 99 -> textView1.text = resources.getText(R.string.more90)
                    sp!!.getString("all_jobs", "") == "0" -> textView1.visibility = View.GONE
                    else -> textView1.text = sp!!.getString("all_jobs", "")
                }
            } else {
                textView1.visibility = View.GONE
            }

            itemView1.addView(notificationBadgeOne)
        }

and as a result I don't see any badge because the item is null but it is visible and clickable. Maybe I made some mistakes somewhere?

UPDATE

I understood where I made a mistake:

if (bottomNavigationView.getChildAt(2) != null) 

this line will always be null. So I removed this condition, but I have an error:

kotlin.TypeCastException: null cannot be cast to non-null type com.google.android.material.bottomnavigation.BottomNavigationItemView

which points on this line:

itemView2.addView(notificationBadge2)

as I understand I make a call to removed item. But I don't know how to check if item is removed or not. Maybe someone knows how to do it?

Andrew
  • 1,947
  • 2
  • 23
  • 61

1 Answers1

1

lets make it easy...:)

https://developer.android.com/reference/com/google/android/material/bottomnavigation/BottomNavigationView

https://material.io/develop/android/components/badging/

implementation 'com.google.android.material:material:1.2.0-alpha03'

style.xml

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

your BottomNavigationView would be like

<com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottom_navigation"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:menu="@menu/bottom_nav_menu"
            />

in activity

val navBar = findViewById<BottomNavigationView>(R.id.bottom_navigation)
var badge = navBar.getOrCreateBadge(R.id.action_add) //R.id.action_add is menu id
badge.number = 2
badge.backgroundColor = //your color
badge.badgeTextColor = // your textcolor
Jaydeep chatrola
  • 2,423
  • 11
  • 17