Load A Remote Image In A Menuitem Using Glide
Usually if I want to load an image with Glide I would write the following: Glide.with(context) .load(theURLOftheImage) .error(R.drawable.ic_error_image) .into(theIma
Solution 1:
Using the approach suggested in the responses for this question worked
@OverridepublicbooleanonPrepareOptionsMenu(Menu menu) {
MenuItem settingsItem = menu.findItem(R.id.actionbar_menu_profile_actions);
if (changeImage) {
Glide.with(this).asBitmap().load(theURLOfTheImage).into(newSimpleTarget<Bitmap>(100,100) {
@OverridepublicvoidonResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
settingsItem.setIcon(newBitmapDrawable(getResources(), resource));
}
});
}
returnsuper.onPrepareOptionsMenu(menu);
}
Solution 2:
Another approach from one of my codings populating a BottomNavigationView:
...
bottomNavigationView = findViewById(R.id.bottomNavigationView);
if(bottomNavigationView != null) {
bottomNavigationView.inflateMenu(R.menu.bottom_main_menu);
MenubottomMenu= bottomNavigationView.getMenu();
//bottomMenu.removeItem(0);finalMenuItemmenuItem= bottomMenu.add("Test 95");
Glide
.with(this)
.load("https:// <add your image resource link here>")
.into(newSimpleTarget<Drawable>() {
@OverridepublicvoidonResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
menuItem.setIcon(resource);
}
});
}
...
remember to add correct Glide version in app gradle, 4.7.1 should work with this one:
implementation 'com.github.bumptech.glide:glide:4.7.1'
Solution 3:
as SimpleTarget is deprecated there is an other solution for someone targeting this page.
Glide
.with(activity)
.load(path)
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.centerCrop()
.listener(newRequestListener<Drawable>() {
@OverridepublicbooleanonLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
returnfalse;
}
@OverridepublicbooleanonResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
menuItem.setIcon(resource);
returnfalse;
}
});
Post a Comment for "Load A Remote Image In A Menuitem Using Glide"