Search code examples
androidandroid-actionbaractionbarsherlock

Android ActionBarSherlock style selected action item


I have a ViewPager AND each page displays a photo. I show a split ActionBar with like/dislike action that are used so user can vote 'like' the photo or 'dislike' it.

I successfully implemented the vote feature for each photo.

Problem:

How do I style the selected vote for each photo? In other words, how can I mange the MenuItem icon based on the current state of the photo?


Solution

  • You should create a boolean to detect when a photo is liked, then call setIcon on your MenuItem.

    final MenuItem liked = YOUR_MENU.findItem(THE_LIKED_ACTION_ID);
        if (isLiked()) {
            liked.setIcon(YOUR_LIKED_DRAWABLE);
        } else {
            liked.setIcon(YOUR_DEFAULT_LIKED_DRAWABLE);
        }
    
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                invalidateOptionsMenu();
            }
        });