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.