I want to set the click listener on button if I'm dynamically adding/removing the view on runtime.
view = LayoutInflater.from(getActivity()).inflate(R.layout.view, null);
button = ButterKnife.findById(view, R.id.button);
container.addView(view);
I tried this:
@Nullable @OnClick(R.id.button)
public void onBtnClick() {
// But this never gets triggered.
}
You have to bind after adding the view. Otherwise use a normal click listener. ButterKnife is not magic, it does findViewById once and sets normal click listeners. If the view isn't there, it won't be set.
Most helpful comment
You have to bind after adding the view. Otherwise use a normal click listener. ButterKnife is not magic, it does
findViewByIdonce and sets normal click listeners. If the view isn't there, it won't be set.