1

I have a view instance. From that I am able to bind views using :

Butterknife.findById(view,R.id.textUser)

But I also need to set click listener on this user text textview. How to achieve it using Butterknife??

1 Answer 1

8

I think you are talking about @OnClick Annotation.

Simple usage:

@OnClick(R.id.textUser)
void userClicked(TextView textView){
//do stuff
}
4
  • This annotation works well when I am doing something inside a activity.But I am doing the binding in a class which is not an activity, but has a view(Basically I am using separate class for initialising the view elements) Oct 28, 2015 at 12:56
  • This is not very elegant to make UI functionality outside an activity. You should avoid doing this tbh. Oct 28, 2015 at 12:59
  • @OnClick works well inside an activity. But it is not working in the class which I am using to initialise the views. For eg I have an activity A. I have another class B. I have passed activity A in the constructor of class B and in class B i was using setOnclickListener and findViewById. This type of structure helps me in proper structuring of my code Oct 28, 2015 at 13:00
  • 1
    Have you tried using Butterknife.bind(activity,view) and then using @Bind annotation and @OnClick? Oct 28, 2015 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.