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:
publicclassLoginActivityextendsActionBarActivity{@InjectView(R.id.toolbar)Toolbar toolbar;@InjectView(R.id.btn_login)Button login;@Overrideprotectedvoid onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState); setContentView(R.layout.login);ButterKnife.inject(this); initialize();//initListeners();@OnClick(R.id.btn_login)publicvoid submit(View view){// TODO submit data to server...}}/*private void initListeners() { @OnClick(R.id.btn_login) public void login(){ } }*/privatevoid 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.
Now do the code to Button. The method should be public and in butterKnife, you won't 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,
@Overrideprotectedvoid onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);ButterKnife.bind(this);}@OnClick(R.id.btn_login)publicvoid submit(View view){//Do your code here. }