I want to switch the icon in the actionbar on clicking to it.I did the following inside onOptionsItemSelected.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.star_School:
if (isStarFilled) {
item.setIcon(R.mipmap.starfilled);
isStarFilled=true;
}else{
item.setIcon(R.mipmap.star);
isStarFilled=false;
}
return true;
}
return super.onOptionsItemSelected(item);
}
This is not working in my case.This is my menu xml file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/star_School"
android:icon="@mipmap/star"
android:title="Bookmark"
app:showAsAction="always" />
</menu>
This is my onCreate Method
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_school_details);
Boolean isStarFilled=false;
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(getIntent().getExtras().getString("name"));
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
cannot resolve symbol isStarFilled? Can somebody please help me?