malli malli - 6 months ago 129
Android Question

ButterKnife onclick is not working

I injected views perfectly using butterknife library.but when i am trying to implement listeners for example onclick but i am not able to implement.following java code will help you clearly.

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 happens.Anything wrong into the code.I already configure the IDE which is compatible to use 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.Thanks in Advance..

Answer

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) {
        if (view.getId() == R.id.button) {

            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"
        android:onClick="submitButton"/>
</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'
}

(or)