Stack Overflow archive
1 score

Loading bitmap to a menu item in a loop with Picasso

score
1
question views
1.1K
license
CC BY-SA 3.0

Create a class where you can keep a strong reference to a Target.

Full working example:

java
public class MainActivity extends AppCompatActivity {

  private final List<Service> services = new ArrayList<>();

  {
    // add arbitrary data just for the example
    services.add(new Service("Android",
        "https://github.com/google/material-design-icons/raw/master/action/drawable-xhdpi/ic_android_black_24dp.png"));
    services.add(new Service("Account",
        "https://github.com/google/material-design-icons/raw/master/action/drawable-xhdpi/ic_account_circle_black_24dp.png"));
    services.add(new Service("Shopping",
        "https://github.com/google/material-design-icons/raw/master/action/drawable-xhdpi/ic_add_shopping_cart_black_24dp.png"));
  }

  @Override public boolean onCreateOptionsMenu(Menu menu) {
    for (Service service : services) {
      // create a few MenuItems. Normally done in XML.
      MenuItem menuItem = menu.add(service.getName());
      menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
      // load the icon using Picasso
      addServiceToMenu(service, menuItem);
    }
    return super.onCreateOptionsMenu(menu);
  }

  private void addServiceToMenu(Service service, final MenuItem menuItem) {
    if (!TextUtils.isEmpty(service.getIconURL())) {
      Resources resources = getResources();
      final int targetWidth = resources.getDimensionPixelSize(R.dimen.menu_icon_size);
      final int targetHeight = resources.getDimensionPixelSize(R.dimen.menu_icon_size);
      final MenuItemIconLoader loader = new MenuItemIconLoader(menuItem, targetHeight, targetWidth);
      loader.load(MainActivity.this, service);
    }
  }

  class MenuItemIconLoader {

    private final WeakReference<MenuItem> itemWeakReference;
    private final int targetHeight;
    private final int targetWidth;

    public MenuItemIconLoader(MenuItem menuItem, int targetHeight, int targetWidth) {
      this.itemWeakReference = new WeakReference<>(menuItem);
      this.targetHeight = targetHeight;
      this.targetWidth = targetWidth;
    }

    private final Target target = new Target() {

      @Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        MenuItem menuItem = itemWeakReference.get();
        if (menuItem != null) {
          Drawable drawable = new BitmapDrawable(getResources(), bitmap);
          drawable.setBounds(0, 0, targetWidth, targetHeight);
          menuItem.setIcon(drawable);
        }
      }

      @Override public void onBitmapFailed(Drawable errorDrawable) {

      }

      @Override public void onPrepareLoad(Drawable placeHolderDrawable) {

      }
    };

    public void load(Context context, Service service) {
      Picasso.with(context).load(service.getIconURL()).resize(targetWidth, targetHeight).into(target);
    }

  }

  static class Service {

    private String name;
    private String iconUrl;

    public Service(String name, String iconUrl) {
      this.name = name;
      this.iconUrl = iconUrl;
    }

    public String getName() {
      return name;
    }

    public String getIconURL() {
      return iconUrl;
    }

  }

}

Originally posted on Stack Overflow. Public user contributions are licensed under Creative Commons Attribution-ShareAlike.