Search code examples
androidandroid-toolbar

Toolbar logo from file


I was trying to add an image from file as the toolbar logo using the following snippet

Picasso.with(toolbar.getContext())
        .load(file)
        .placeholder(R.drawable.ic_action_camera)
        .error(R.drawable.ic_action_camera)
        .transform(new CircleTransform())
        .into(target);

and the target is

target = new Target() {
    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        Logger.d("HIYA", "onBitmapLoaded");
        Bitmap b = Bitmap.createScaledBitmap(bitmap, 120, 120, false);
        BitmapDrawable icon = new BitmapDrawable(toolbar.getResources(), b);
        toolbar.setLogo(icon);
    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {
        Logger.d("HIYA", "onBitmapFailed");
    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {
        Logger.d("HIYA", "onPrepareLoad");
    }
};

Solution

  • Try the following link

    final ActionBar ab = getSupportActionBar();
    Picasso.with(this)
        .load(imageURL)
        .into(new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                Drawable d = new BitmapDrawable(getResources(), bitmap);
                ab.setIcon(d);
                ab.setDisplayShowHomeEnabled(true);
                ab.setDisplayHomeAsUpEnabled(true);
            }
    
            @Override
            public void onBitmapFailed(Drawable errorDrawable) {
            }
    
            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {
            }
       });
    

    This is worked for me.