Butterknife: How to Use ButterKnife.bind() in a non activity class

Created on 30 Jul 2016  Â·  9Comments  Â·  Source: JakeWharton/butterknife

I am using a class that implements TextWatcher to validate my fields in Sign up page and i want to use butterknife to bind views. How can i do it. I have passed the instance of my Activity class to my TextWatcher implementation but after that also it doesn't bind the views..What can i do?

butterknife

And the call from Activity

butterknife2

Most helpful comment

Call ButterKnife.bind(this, this.activity) instead of ButterKnife.bind(this.activity) in the SignUpValidation. The first argument is the target for your bindings, the second argument is the source of the bindings.

All 9 comments

Use the two argument version which separates the instance being bound (this) from the source of the views (activity).

In my code how to do that?Can you guide please?

Call ButterKnife.bind(this, this.activity) instead of ButterKnife.bind(this.activity) in the SignUpValidation. The first argument is the target for your bindings, the second argument is the source of the bindings.

It worked fine..Thank you very much..

Sorry for resurfacing this old thread, but in the new version of ButterKnife, there is now the ability to bind resources, such as with @BindColor() - is this possible to do in a custom view as long as I have a context object. I know I can pass along my activity reference, but I'd just rather not add an unnecessary extra parameter.

Yes. Reference the generated class directly and it has a constructor that
accepts only a Context.

On Wed, Feb 22, 2017, 8:38 PM Edward Lee notifications@github.com wrote:

Sorry for resurfacing this old thread, but in the new version of
ButterKnife, there is now the ability to bind resources, such as with
@BindColor() - is this possible to do in a custom view as long as I have a
context object. I know I can pass along my activity reference, but I'd just
rather not add an unnecessary extra parameter.

—
You are receiving this because you modified the open/close state.

Reply to this email directly, view it on GitHub
https://github.com/JakeWharton/butterknife/issues/694#issuecomment-281863312,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAEEES57BvTdJzFJjgF3zDBvv_f4quqSks5rfOMfgaJpZM4JYz6e
.

Hi @JakeWharton thanks for the quick reply. I'm not sure what you mean by the generated class. Which class should I be looking at because ButterKnife.bind doesn't have a signature that accepts only a Context:

screenshot 2017-02-22 20 27 05

SideScroller_ViewBinder

On Wed, Feb 22, 2017 at 11:28 PM Edward Lee notifications@github.com
wrote:

Hi @JakeWharton https://github.com/JakeWharton thanks for the quick
reply. I'm not sure what you mean by the generated class. Which class
should I be looking at because ButterKnife.bind doesn't have a signature
that accepts only a Context:

[image: screenshot 2017-02-22 20 27 05]
https://cloud.githubusercontent.com/assets/1836653/23244923/6ddb8b48-f93d-11e6-963e-68242aadf5ef.png

—
You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub
https://github.com/JakeWharton/butterknife/issues/694#issuecomment-281891538,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAEEEfgUm1pOCz6-Ag_e0eNJ6hHGDHrTks5rfQregaJpZM4JYz6e
.

Oh shit. Thanks for making this library and helping me make my code a little cleaner.

Was this page helpful?
0 / 5 - 0 ratings