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..
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' }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With