-1

I'm using MaterialDrawer lib to generate a Navigation Drawer in Android, I'm also trying to get an image from an external source and set it as an icon in the DrawerItem object and for that I'm using Glide v4 lib to download the image from the external source but not sure how would it update the icon placeholders in each and every DrawerItem I have in the Drawer. Here's what I've done so far:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    ...

    ArrayList<IDrawerItem> drawerItems = new ArrayList<IDrawerItem>();
    String icon = "http://myiconwebsite.com/myicon.png";

    //create Drawer Item obj
    PrimaryDrawerItem primary = new PrimaryDrawerItem();

    //set title
    primary.withName("Test");
    primary.withIdentifier(1);
    primary.withSelectable(false);

    //define image view for Glide
    //I'm defining this image view so I can load the downloaded image into it so I can get the loaded image as Drawable
    final ImageView iv = new ImageView(this);

    //load the image in the view
    Target rb = Glide.with(this).asDrawable().load(icon).into(iv);

    //get the drawable object from the image view
    Drawable dr = iv.getDrawable();

    //set the icon of this drawer
    primary.withIcon(dr);

    //add it to the list of drawers
    drawerItems.add(primary);

    //create our drawer
    DrawerBuilder result = new DrawerBuilder();
    result.withActivity(this);
    result.withDrawerItems (drawerItems);

    ...

    result.build();

    ...

}

Notice that I'm trying to download the image using Glide and load the image inside an ImageView and then extract that image as a Drawable object using iv.getDrawable(). I'm doing it that way because I need the object to be Drawable as that's what the method primary.withIcon(dr) accepts in order to show that icon in the drawer. Now the problem is when I call iv.getDrawable() it returns null and doesn't work as expected. How to achieve that?

also I might be doing something wrong here? as I saw in the documentations they mentioned libs like Glide and Picasso can be supported but they didn't add enough examples of how to do that. Bear in mind I'm still new to Java and Glide so I'm not sure if that's the way to do it.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Desolator
  • 22,411
  • 20
  • 73
  • 96

1 Answers1

0

I would not recommend loading a Drawable from the server on the UI thread. In addition I assume that this

Target rb = Glide.with(this).asDrawable().load(icon).into(iv);
Drawable dr = iv.getDrawable();

will always return null at time of the access.

To load an icon from a server for the normal drawer items, please use the DrawerImageLoader implementation as shown in the sample for the profiles (you might need to do small adjustments as the v4 glide API changed a lot)

For being able to load an icon from a server for the normal items, you will have to add a CustomDrawerItem as this is not supported by default. The sample app has a sample implementation which can be used for example:

https://github.com/mikepenz/MaterialDrawer/blob/develop/app/src/main/java/com/mikepenz/materialdrawer/app/drawerItems/CustomUrlPrimaryDrawerItem.java

mikepenz
  • 12,708
  • 14
  • 77
  • 117
  • Thanks I've eventually figured that out, although it would be nice to have that built in all of the drawerItem objects by default. I kinda didn't like creating new classes to extend the base DrawerItem to support this feature for each and every DrawerItem type. Maybe I'm just lazy. but I like to keep it clean so I preloaded all of the Icons using a splash screen activity and stored their drawables in a static Hashmap. Once the process is done it will take me to main activity, now In the main activity that contains the drawer all I did I mapped these cached drawables to my drawerItems and done. – Desolator Aug 24 '17 at 17:54