Skip to content Skip to sidebar Skip to footer

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?.

Solution 1:

Firstly, you should enable two settings like this:

getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayUseLogoEnabled(true);

Then you can use .asDrawable() along with CustomTarget<Drawable>() like this:

Glide.with(this).asDrawable().load(imageUrl).into(new CustomTarget<Drawable>() {
        @Override
        public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
            getSupportActionBar().setLogo(resource);
        }

        @OverridepublicvoidonLoadCleared(@Nullable Drawable placeholder) {

        }
    });

Notes:

  • Make sure to replace the context (this) and imageUrl with your data.
  • Don't use SimpleTarget as it is deprecated now starting from version 4.8.0.

Solution 2:

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.

Post a Comment for "Set Icon On Actionbar Using Glide"