learner
learner

Reputation: 1321

How to add @OnClick from buttom inside a Layout

I'm using ButterKnife to simplify my life and I have a button inside a LinearLayout which I would like to include @OnClick annotation on it.

<LinearLayout
    android:id="@+id/buttons_container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingTop="30dp"
    android:layout_centerHorizontal="true">
    <Button
        android:id="@+id/cancel_button"
        style="@style/PaymentButtonStyle"
        android:text="calncel@string/cancel_label"
        android:textSize="15sp"/>
  </LinearLayout>

With this layout it complains the button ID isn't found.

@OnClick(R.id.cancel_payment_button)
public void onCancelClick(View target) {
        ...
    }

What do I need to do?

Upvotes: 0

Views: 247

Answers (2)

Luiz Fernando Salvaterra
Luiz Fernando Salvaterra

Reputation: 4182

I think it is not finding the button with the correct id, in the layout that you included above there is no id for R.id.cancel_payment_button, only R.id.cancel_button. Also, check if the import for R class is correct (following your main package).

Upvotes: 0

OlehKykena
OlehKykena

Reputation: 64

Your id is cancel_button, not a cancel_payment_button.

And also don't forget to bind ButterKnife with ButterKnife.bind() method.

Upvotes: 2

Related Questions