Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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(theImageView);

but what if I need to load the image of that URL into a MenuItem that has to be changed in real time?

The following is not possible because the method into does not accept the parameter:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem settingsItem = menu.findItem(R.id.actionbar_menu_profile_actions);
    if (changeImage) {
        Glide.with(this).load(theURLOftheImage).error(R.drawable.ic_error_image).into(settingsItem);
    }
    return super.onPrepareOptionsMenu(menu);
}
like image 908
AlvaroSantisteban Avatar asked May 09 '16 12:05

AlvaroSantisteban


People also ask

How do you load a drawable image with Glide?

into(myImageView); Load method of Glide also accept drawable object as a parameter to load image. You just have to get the drawable object from your drawable image and pass it to the load method.

How do you download images from URL on Glide Android?

To simply load an image to LinearLayout, we call the with() method of Glide class and pass the context, then we call the load() method, which contains the URL of the image to be downloaded and finally we call the into() method to display the downloaded image on our ImageView.


2 Answers

Using the approach suggested in the responses for this question worked

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem settingsItem = menu.findItem(R.id.actionbar_menu_profile_actions);
    if (changeImage) {
         Glide.with(this).asBitmap().load(theURLOfTheImage).into(new SimpleTarget<Bitmap>(100,100) {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
                settingsItem.setIcon(new BitmapDrawable(getResources(), resource));
            }
        });
    }
    return super.onPrepareOptionsMenu(menu);
}
like image 120
AlvaroSantisteban Avatar answered Oct 24 '22 19:10

AlvaroSantisteban


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);

        Menu bottomMenu = bottomNavigationView.getMenu();

        //bottomMenu.removeItem(0);

        final MenuItem menuItem = bottomMenu.add("Test 95");

        Glide
                .with(this)
                .load("https:// <add your image resource link here>")
                .into(new SimpleTarget<Drawable>() {
                    @Override
                    public void onResourceReady(@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'
like image 37
Roar Grønmo Avatar answered Oct 24 '22 21:10

Roar Grønmo