2

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.

  • Post the relevant code in your question. – Sufian Nov 15 '15 at 11:11
  • and explain what you mean by 'It doesn't work". – Sufian Nov 15 '15 at 11:12
  • What they MIGHT have meant was when using Picasso to load the image into the header view image field of the Android Studio's canned generated code using design libraries NavigationView, if you fire up Picasso to retrieve an image in onCreate of the activity, Picasso fetches the image and assigns the image to the imageview but when the draw slides open it isn't assigned anymore. No idea why, I'm trying to determine the problem too. – Kenny Apr 14 '16 at 22:00
  • Hunted down my own problem, might have been theirs. I was allocating a Target in the "into" part of the request builder and not saving a reference and it was garbage collected. I held a reference and all was good. – Kenny Apr 14 '16 at 22:13

2 Answers2

0

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);
Yogesh Shinde
  • 393
  • 4
  • 11
0

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'
Er. Harsh Rathore
  • 758
  • 1
  • 7
  • 21