I have this layout with this heart button, so inside the click event of this button I change the icon
IwantedtochangemyhearttherebythisheartwhenIclick
The answer below solved more than one problem
You need to override the onCreateOptionsMenu and onOptionsItemSelected methods
1) Retain the desired MenuItem:
private MenuItem menuCoracao;
...
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.seu_menu, menu);
menuCoracao = menu.findItem(R.id.id_do_seu_menu);
return super.onCreateOptionsMenu(menu);
} 2) Detect the menu click and change the icon:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.id_do_seu_menu) {
menuCoracao.setIcon(R.drawable.seu_drawable);
}
return super.onOptionsItemSelected(item);
}
I've tried using onCreate so
meuCoracao.setIcon(R.drawable.ic_action_favorite_v);
gave this error
E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{praias.android.makerapp.com.praias/praias.android.makerapp.com.praias.MainActivity}: java.lang.NullPointerException