2

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?

seon
  • 1,050
  • 2
  • 13
  • 27

1 Answers1

0

Keep your icons in drawable and compare some thing like this

 mRememberPwd.getDrawable().getConstantState().equals
  (getResources().getDrawable(R.drawable.login_checked).getConstantState())

you can find more answers here on how to compare drawable

you can also try using flag

global variable

boolean  isStarFilled=false;


case R.id.star_School:
            if (isStarFilled) {
                item.setIcon(R.mipmap.starfilled);
             isStarFilled=false;
            }else{
            item.setIcon(R.mipmap.star);
               isStarFilled=true;
               }
            return true;
Community
  • 1
  • 1
Manohar
  • 22,116
  • 9
  • 108
  • 144
  • variable isStarfilled is not connected to anywhere.How to initialize isStarFilled? – seon Nov 17 '16 at 05:19
  • keep it on top of oncreate method – Manohar Nov 17 '16 at 05:20
  • I have edited my question with latest code.Shows error inside if (isStarFilled) cannot resolve symbol isStarFilled – seon Nov 17 '16 at 05:27
  • keep boolean isStarFilled=false; on top of oncreate, not inside oncreate method – Manohar Nov 17 '16 at 05:28
  • I did as u said.It neither displays any error nor the icon is changed. – seon Nov 17 '16 at 05:32
  • Thank you sir.Thank you so much.You have been very helpful.Can you do me a favour one more time?When i set the star to filled and come out of activity and again opened that activity star becomes blank again.I wish to remain as it is unless I change by myself?How can I do this? – seon Nov 17 '16 at 05:40
  • if you want to save permanently what was your previous icon , use sharedpreferences to store what was your previous value of isstared and retrevie it back from this activity every time, do some research on how to store data in sharedpreferences and retriving it back, If my answer helped you please up vote it and consider accepting my answer by clicking tick mark beside my answer :) – Manohar Nov 17 '16 at 05:48