26

I injected views perfectly using butterknife library. But when I try to implement listeners, for example onclick I'm not able to implement them. Following java code will help you to understand my problem.

Java code:

public class LoginActivity extends ActionBarActivity{
    @InjectView(R.id.toolbar) Toolbar toolbar;
    @InjectView(R.id.btn_login) Button login;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        ButterKnife.inject(this);

        initialize();
        //initListeners();

        @OnClick(R.id.btn_login)
        public void submit(View view) {
          // TODO submit data to server...
        }
    }

    /*private void initListeners() {
        @OnClick(R.id.btn_login)
        public void login(){

        }
    }*/

    private void initialize() {
        setSupportActionBar(toolbar);
        getSupportActionBar().setIcon(R.drawable.toolbar_icon);
        getSupportActionBar().setTitle(null);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }
}

Tell me why it is happening. Anything wrong in code? I've already configured the IDE which is compatible with ButterKnife by using following URL.

http://stackoverflow.com/questions/27754811/onclick-is-not-working-in-implementation-of-butterknife-library

Please give me any suggestions regarding this issue. Thanks in Advance..

Marat
  • 6,142
  • 6
  • 39
  • 67
malli
  • 642
  • 2
  • 12
  • 30

5 Answers5

17

MainActivity.java

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;


public class MainActivity extends ActionBarActivity {


    @InjectView(R.id.button)
    Button login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);


    }


    @OnClick(R.id.button)
    void submitButton(View view) {
            Toast.makeText(this, "Click", Toast.LENGTH_SHORT).show();
    }
}

and the activity_main.xml part

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="59dp"
        android:layout_marginStart="59dp"
        android:layout_marginTop="132dp"
        />
</RelativeLayout>

in build.gradle file(app)

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.0.0'
    compile 'com.jakewharton:butterknife:6.1.0'
}
vinay Maneti
  • 1,447
  • 1
  • 23
  • 31
  • vinay i am not implementing android listeners.I need to do it with butterknife library.. – malli Apr 13 '15 at 11:15
  • but you have implemented normal android listener.i want to be implement listener with butterknife only.like following @OnClick(R.id.submit) void submit() { // TODO call server... } – malli Apr 13 '15 at 11:35
  • Yes i have seen that see following url contains butterknife sample.i want listeners to be implement as it is..https://github.com/JakeWharton/butterknife – malli Apr 13 '15 at 11:38
  • 2
    I still can use it even without "@OnClick" since it's linked using the xml: android:onClick="submitButton" – nafsaka Aug 01 '16 at 06:13
  • yes. It works without "@OnClick", as we are using in the xml file for button "android:onClick="submitButton"" – vinay Maneti Aug 01 '16 at 09:01
  • 2
    but if you can use onClick() en the xml no it's necesary butterknife it's incorrect this answerd. – David Hackro Aug 04 '16 at 17:26
  • Ok i saw there was onClick implemented in XML as well, can't see it now. So that's why i said that because it doesn't make sense to implement onClick in XML and then implementing it through Butterknife as well. But still in this changed code you are comparing the view id, it isn't really necessary because you have already provided id to butterknife in annotation so it will take care of that, and only the associated view will receive the event. – Saini Arun Apr 19 '18 at 16:46
  • 1
    this answer is missing { annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0' } – abdullahicyc May 03 '18 at 07:40
10

In my case this is my solution

add classpath in gradle(Project)

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.0'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}

and gradle(Module) add apply and apt

apply plugin: 'com.neenbedankt.android-apt'
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'   
    compile 'com.jakewharton:butterknife:8.2.1'
    apt 'com.jakewharton:butterknife-compiler:8.2.1'
}

file java

@OnClick(R.id.btn_login)
public void submit(View view) {
  // TODO submit data to server...
}
David Hackro
  • 3,652
  • 6
  • 41
  • 61
7

You should've to bind butterKnife before you use the annotations.

Add these dependencies to gradle.build

compile 'com.jakewharton:butterknife:8.4.0' annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'

Then add bind to onCreate ButterKnife.bind(this);

Now do the code to Button. The method should be public and in butterKnife, you not required to add the onClick in XML. And method also should be outside of onCreate It'll automatically get button which you assign using the annotation given at above the method,

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);

}
@OnClick(R.id.btn_login)
public void submit(View view){ 

   //Do your code here. 

}
Fahry Mohammed
  • 599
  • 5
  • 18
4

make sure you add all required dependencies

dependencies {
compile 'com.jakewharton:butterknife:8.4.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
}
  • The OP mentions (in a comment) that he is getting compiler errors about different method signatures, not about missing dependencies. – Robert Dec 21 '16 at 17:55
  • no the Q about ButterKnife onclick is not working i have face same issue and solved by adding missing dependency – Zeinab Abdelmawla Dec 21 '16 at 18:51
2

You have to move your @OnClick out of the onCreate method, as i did below in the code snippet.

The code i posted below should work as it's supposed to (I also use ButterKnife).

public class LoginActivity extends ActionBarActivity{
    @InjectView(R.id.toolbar) Toolbar toolbar;
    @InjectView(R.id.btn_login) Button login;

    @OnClick(R.id.btn_login)
    public void submit(View view) {
       // TODO submit data to server...
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        ButterKnife.inject(this);

        initialize();
    }

    private void initialize() {
        setSupportActionBar(toolbar);
        getSupportActionBar().setIcon(R.drawable.toolbar_icon);
        getSupportActionBar().setTitle(null);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }
}
Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53
Moonbloom
  • 7,738
  • 3
  • 26
  • 38