2

I wanted to load image from url into bottomnavigationview's menu item icon. same like instagram we wanted to show profile image in one of our menu which might change while switching from different available profiles. Currently, I am trying below code but it does not load image every time.

val menu = mViewDataBinding?.bnvNavigation?.menu
    val menuItem = menu?.findItem(R.id.menu_profile)

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        menuItem?.setIconTintList(null)
        menuItem?.setIconTintMode(null)
    }


    Glide.with(this)
        .asBitmap()
        .load(profilePic)
        .apply(
            RequestOptions.circleCropTransform()
                .placeholder(R.drawable.user_profile_icon_updated)
        )
        .into(object :
            CustomTarget<Bitmap>(UIHelper.dpToPx(this, 24), UIHelper.dpToPx(this, 24)) {
            override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                menuItem?.icon = BitmapDrawable(resources, resource)
              }

            override fun onLoadCleared(placeholder: Drawable?) {
            }
        })
P Vartak
  • 434
  • 5
  • 17

2 Answers2

3

Above code is working as expected, it just a matter of passing application context to Glide, Glide.with(App.instance)

App is my application class

val menu = mViewDataBinding?.bnvNavigation?.menu
    val menuItem = menu?.findItem(R.id.menu_profile)
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        menuItem?.setIconTintList(null)
        menuItem?.setIconTintMode(null)
    }
    Glide.with(App.instance)
        .asBitmap()
        .load(profilePic)
        .placeholder(getProfilePlaceholderFilled())
        .error(getProfilePlaceholderFilled())
        .apply(
            RequestOptions.circleCropTransform().placeholder(getProfilePlaceholderFilled())
        )
        .into(object :
            CustomTarget<Bitmap>(UIHelper.dpToPx(this, 80), UIHelper.dpToPx(this, 80)) {
            override fun onResourceReady(
                resource: Bitmap,
                transition: Transition<in Bitmap>?
            ) {
                menuItem?.icon = BitmapDrawable(
                    resources, resource
                )
            }

            override fun onLoadFailed(errorDrawable: Drawable?) {
                menuItem?.icon = errorDrawable
            }

            override fun onLoadCleared(placeholder: Drawable?) {
            }
        })
P Vartak
  • 434
  • 5
  • 17
0

i recommended you to use chip navigation and create custom bottom navigation its very better than default android bottom navigation and more easy...

if you interest to learn you can use this tutorial https://www.youtube.com/watch?v=DQtdOSN21lQ&feature=emb_logo and the library https://github.com/ismaeldivita/chip-navigation-bar

if you have any question tag me to answer you

MHmansouri
  • 75
  • 11
  • Interesting. but as I navigate through lib page and issues I found this. https://github.com/ismaeldivita/chip-navigation-bar/issues/48 which means lib does not allow to set drawable icon or dynamic icon over the web directly. So no use for my case – P Vartak Jan 29 '21 at 04:37