Search code examples
androidandroid-actionbarandroid-glide

Set icon on ActionBar using glide


I am using glide to load image URLs in imageviews of recycler view and it works fine.

Now, I want to set ActionBar icon from a dynamic image URL. How can I achieve it using glide?.

I have referred following link: How to load a circular appcompat actionbar logo using glide. But,I did not get the result. and I don't want to switch to picasso.

Can Anyone Help me?

My Code:

 Glide.with(this)
            .load(receiver1.profilepic)
            .placeholder(R.drawable.user)
            .into(new Target<GlideDrawable>()
            {
                @Override
                public void onLoadStarted(Drawable placeholder)
                {

                }

                @Override
                public void onLoadFailed(Exception e, Drawable errorDrawable)
                {

                }

                @Override
                public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation)
                {

                    Bitmap bitmap1=((GlideBitmapDrawable) resource).getBitmap();

                    Drawable drawable = new BitmapDrawable(getResources(), bitmap1);
                    actionBar.setIcon(drawable);
                }

                @Override
                public void onLoadCleared(Drawable placeholder)
                {

                }

                @Override
                public void getSize(SizeReadyCallback cb) {

                }


                @Override
                public void setRequest(com.bumptech.glide.request.Request request)
                {

                }

                @Override
                public com.bumptech.glide.request.Request getRequest()
                {
                    return null;
                }

                @Override
                public void onStart()
                {

                }

                @Override
                public void onStop()
                {

                }

                @Override
                public void onDestroy()
                {

                }
            });

Solution

  • First, we set getSupportActionBar().setDisplayShowHomeEnabled(true) and getSupportActionBar().setDisplayUseLogoEnabled(true) and then after managing the action bar icon visibility, just use actionBar.setIcon(bitmap) or actionBar.setIcon(drawable) to render the loaded image.