Malwinder Singh
Malwinder Singh

Reputation: 7060

ButterKnife Android: Unable to add Listener to View

I added Butter knife library to Gradle like this:

dependencies {
   compile 'com.jakewharton:butterknife:8.0.1'
   ...
}

Created a Button with id btnPress. In my Activity, when i tried to add method with @onClick(R.id.btnPress), on running the application, the method does not execute.

Activity:

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.btnPress)
    Button btnPress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        ButterKnife.bind(MainActivity.this);
    }

    //This method is not being called when Button is pressed.
    @OnClick(R.id.btnPress)
    void onPress() {
        ...
    }
}

Upvotes: 1

Views: 475

Answers (1)

Malwinder Singh
Malwinder Singh

Reputation: 7060

Here's how i resolved this issue:

First, in your top-level build.gradle file, include:

classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'

in the buildscript dependencies, such as:

  buildscript {
    dependencies {
      classpath 'com.android.tools.build:gradle:2.0.0'
      classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
  }

Then, in your module's build.gradle file, include apply plugin: 'com.neenbedankt.android-apt' towards the top.

Now include ButterKnife library and compiler in module level build.gradle:

dependencies {
    compile 'com.jakewharton:butterknife:8.0.1'
    apt 'com.jakewharton:butterknife-compiler:8.0.1'
    ...
}

Upvotes: 1

Related Questions