Search code examples
androidandroid-drawablebutterknife

ButterKnife - bind drawable resource


How to eliminate the following initialization code using ButterKnife annotations?

private Drawable mExpandDrawable;
private Drawable mCollapseDrawable;

void init() {
    mExpandDrawable = getResources().getDrawable(R.drawable.ic_expand_small_holo_light);
    mCollapseDrawable = getResources().getDrawable(R.drawable.ic_collapse_small_holo_light);
}

Solution

  • Use @BindDrawable from ButterKnife 7 API.

    import butterknife.BindDrawable;
    
    @BindDrawable(R.drawable.ic_expand_small_holo_light)
    protected Drawable mExpandDrawable;
    @BindDrawable(R.drawable.ic_collapse_small_holo_light)
    protected Drawable mCollapseDrawable;
    
    void init() {
        ButterKnife.bind(this);
    }
    

    There are @BindString, @BindInt, @BindDimen, @BindColor, @BindBool for other resource types.