UPDATE: I switched to Glide and now it works
I have a NavigationView (Drawer) with programmatically created MenuItems. Is it possible to load the icons for them with Picasso? I tried it but it doesn't work. I tried it using a Target from Picasso.
UPDATE: I switched to Glide and now it works
I have a NavigationView (Drawer) with programmatically created MenuItems. Is it possible to load the icons for them with Picasso? I tried it but it doesn't work. I tried it using a Target from Picasso.
We have to get the 'NavigationView' by the ID and then apply the 'picasso' image using the 'NavigationView' object.
NavigationView navView =(NavigationView)findViewById(R.id.nav_view);
navView.setNavigationItemSelectedListener(this);
ImageView header=(ImageView)navView.getHeaderView(0).findViewById(R.id.event_logo);
Picasso.with(this).load( "https://d30y9cdsu7xlg0.cloudfront.net/png/17241-200.png").into(header);
I recently found a way to do this with Picasso in kotlin language
//Implemented Databinding in project so able to
//call views directly with the help of binding object
val menu: Menu = binding.navigationView.menu
//Picasso form com.squreup.picasso.Picasso
Picasso.get().load("""**SOURCE URL**""")
.placeholder(R.drawable.<placeholder_name>)
.into(object: Target{ //Target interface from com.squareup.picasso.Target
override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) {
println("icon loaded $bitmap")
menu[0].icon = BitmapDrawable(resources, bitmap)
}
override fun onBitmapFailed(e: Exception?, errorDrawable: Drawable?) {
println("Loading failed... ${e?.message}")
}
override fun onPrepareLoad(placeHolderDrawable: Drawable?) {
println("Loading your icon...")
menu[0].icon = placeHolderDrawable
}
})
For java there will be a little bit different code
//Implemented Databinding in project so able to
//call views directly with the help of binding object
Menu menu = binding.navigationView.getMenu();
//Picasso form com.squreup.picasso.Picasso
Picasso.get().load("""**SOURCE URL**""")
.placeholder(R.drawable.<placeholder_name>)
.into(new Target(){ //Target interface from com.squareup.picasso.Target
@Override
fun onBitmapLoaded(@Nullable Bitmap bitmap, @Nullable Picasso.LoadedFrom from) {
System.out.println("icon loaded " + bitmap.toString())
menu[0].setIcon(new BitmapDrawable(getResources(), bitmap))
}
@Override
fun onBitmapFailed(@Nullable Exception e, @Nullable Drawable errorDrawable) {
System.out.println("Loading failed... " + e.getMessage())
}
@Override
fun onPrepareLoad(@nullable Drawable placeHolderDrawable) {
System.out.println("Loading your icon...")
menu[0].setIcon(placeHolderDrawable)
}
})
Note: Please take care to implement Picasso dependency in you app level build.gradle file
//latest_version is 2.71828 at the time of posting this answer
implementation 'com.squareup.picasso:picasso:$latest_version'