ButterKnife onclick is not working
Asked Answered
M

5

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.

https://mcmap.net/q/536694/-onclick-is-not-working-in-implementation-of-butterknife-library

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

Mclemore answered 13/4, 2015 at 11:7 Comment(5)
can you post your stack TrackHashimoto
Put "submit" method outside oncreateAleda
hi vinay it is giving "void is an invalid type for the variable submit"Mclemore
hi piotr you can check it with butterknife github code.so that you can get more clarity on this..Mclemore
This is the error which i am getting at compile time.please have look into it."Multiple markers at this line - void is an invalid type for the variable submit - Syntax error on token ")", ; expected - Syntax error on token "(", ; expected"Mclemore
H
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'
}
Hashimoto answered 13/4, 2015 at 11:12 Comment(8)
vinay i am not implementing android listeners.I need to do it with butterknife library..Mclemore
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... }Mclemore
Yes i have seen that see following url contains butterknife sample.i want listeners to be implement as it is..github.com/JakeWharton/butterknifeMclemore
I still can use it even without "@OnClick" since it's linked using the xml: android:onClick="submitButton"Xanthic
yes. It works without "@OnClick", as we are using in the xml file for button "android:onClick="submitButton""Hashimoto
but if you can use onClick() en the xml no it's necesary butterknife it's incorrect this answerd.Metamorphic
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.Polecat
this answer is missing { annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0' }Melchizedek
M
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...
}
Metamorphic answered 4/8, 2016 at 17:34 Comment(0)
S
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. 

}
Smelt answered 19/9, 2017 at 6:3 Comment(0)
R
4

make sure you add all required dependencies

dependencies {
compile 'com.jakewharton:butterknife:8.4.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
}
Rundown answered 21/12, 2016 at 17:28 Comment(2)
The OP mentions (in a comment) that he is getting compiler errors about different method signatures, not about missing dependencies.Romilly
no the Q about ButterKnife onclick is not working i have face same issue and solved by adding missing dependencyRundown
E
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);
    }
}
Ecumenicity answered 13/4, 2015 at 11:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.