Farid Abdi
Farid Abdi

Reputation: 99

How to change hamburger menu icon in navigation drawer from URL by using Picasso?

I want my app receive an image from url by using picasso and convert this image to standard icons and set it as hamburger menu icon in navigation drawer . I have a code that do that from res/mipmap folder very good . But in this state I shoud convert my image to standard android icons using ( for example Android Asset Studio ) manualy and store them in mipmap folders manualy .

This is the the code that do that very good :

getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.mipmap.ic_farid);

I then find the code that do this from url by picasso . This code works but the hamburger icon shape is corrupted and very large and bad .

This is the code :

{
        final Target mTarget = new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
                Log.d("DEBUG", "onBitmapLoaded");
                 mBitmapDrawable = new BitmapDrawable(getResources(), bitmap);                                     
            }

            @Override
            public void onBitmapFailed(Exception e, Drawable errorDrawable) {

            }
            @Override
            public void onPrepareLoad(Drawable drawable) {
                Log.d("DEBUG", "onPrepareLoad");
            }
        };
        Picasso.get().load("http://192.168.1.53:8080/Farid/1.jpg").into(mTarget);

}
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeAsUpIndicator(mBitmapDrawable);

How I could solve this problem ? I want , when image loaded from url , is converted to standard android icons . Thanks

Upvotes: 0

Views: 287

Answers (2)

Lokik Soni
Lokik Soni

Reputation: 702

try this for specifying image height and width

Picasso.with(context)
             .load(uri).resize(100, 100).centerCrop()
             .placeholder(R.drawable.type_pic)
             .error(R.drawable.type_pic)
             .into(holder.icon);

Upvotes: 0

Chrisvin Jem
Chrisvin Jem

Reputation: 4060

You need to put setHomeAsUpIndicator inside onBitmapLoaded.

{
        final Target mTarget = new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
                Log.d("DEBUG", "onBitmapLoaded");
                 Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, 50, 50, false);
                 mBitmapDrawable = new BitmapDrawable(getResources(), scaledBitmap);
                 getSupportActionBar().setHomeAsUpIndicator(mBitmapDrawable);
            }

            @Override
            public void onBitmapFailed(Exception e, Drawable errorDrawable) {

            }
            @Override
            public void onPrepareLoad(Drawable drawable) {
                Log.d("DEBUG", "onPrepareLoad");
            }
        };
        Picasso.get().load("http://192.168.1.53:8080/Farid/1.jpg").into(mTarget);

}
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Upvotes: 1

Related Questions