Butterknife: Setting click listener on dynamically added views

Created on 24 Sep 2015  Â·  1Comment  Â·  Source: JakeWharton/butterknife

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.
}

Most helpful comment

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.

>All comments

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.

Was this page helpful?
0 / 5 - 0 ratings