Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8943141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:42:02+00:00 2026-06-15T11:42:02+00:00

public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: return true; case R.id.searchIcon:

  • 0
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

    case android.R.id.home:
        return true;

    case R.id.searchIcon:
        return true;

    case R.id.startRefresh:
        refreshItem = item;
        refresh();
        return true;
    case R.id.stopRefresh:

        if (refreshItem != null && refreshItem.getActionView() != null) {
            refreshItem.getActionView().clearAnimation();
            refreshItem.setActionView(null);
        }
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}


public void refresh() {
    if (FeedActivity.this != null) {
        /*
         * Attach a rotating ImageView to the refresh item as an ActionView
         */
        LayoutInflater inflater = (LayoutInflater) FeedActivity.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ImageView iv = (ImageView) inflater.inflate(
                R.layout.refresh_action_view, null);
        Animation rotation = AnimationUtils.loadAnimation(
                FeedActivity.this, R.anim.clockwise_refresh);
        rotation.setRepeatCount(Animation.INFINITE);
        iv.startAnimation(rotation);
        refreshItem.setActionView(iv);
    }
}

Before Clicking:

enter image description here

After Clicking:

enter image description here

Here the icon is being animated(rotating).

Problem:

why is it shifting to the left?

once it shifts to the left, the icon becomes non clickable and strangely the device back button also doesn’t work

EDIT:

In comments below this answer:

Animated Icon for ActionItem

Jake Warton says if you are using a square and correct sized icon for the menu item, you wont get this weird behaviour, to someone who has the same problem.

But i am using a 32×32 image on a device which uses mdpi drawables. Which as stated there must work 🙁

Thank You

EDIT:

refresh_action_view.xml

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/Widget.Sherlock.ActionButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_refresh" />

Custom Style i use in my app

<style name="My_solid_ActionBar" parent="@style/Widget.Sherlock.Light.ActionBar.Solid.Inverse">
    <item name="background">@drawable/ab_solid_My</item>
    <item name="backgroundStacked">@drawable/ab_stacked_solid_My</item>
    <item name="backgroundSplit">@drawable/ab_bottom_solid_My</item>
    <item name="progressBarStyle">@style/My_ProgressBar</item>
    <item name="android:background">@drawable/ab_solid_My</item>
    <item name="android:backgroundStacked">@drawable/ab_stacked_solid_My</item>
    <item name="android:backgroundSplit">@drawable/ab_bottom_solid_My</item>
    <item name="android:progressBarStyle">@style/My_ProgressBar</item>
</style>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:42:03+00:00Added an answer on June 15, 2026 at 11:42 am

    The issue is that you’re not handling all menu inflation in onCreateOptionsMenu(). The basic logic for an ActionBar refresh animation I’ve seen used in apps with open source , for example Andlytics (and also used myself in projects), is to implement a boolean flag in onCreateOptionsMenu() to decide whether to show the refresh animation.

    You can implement it like this: When your refresh() method is called, it sets the boolean isRefreshing flag to true and calls inValidateOptionsMenu() which ‘behind the scene’ calls onCreateOptionsMenu() to start the animation:

    Inflate the menu in onCreateOptionsMenu(...):

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        menu.clear();
        super.onCreateOptionsMenu(menu, inflater);
        //inflate a menu which shows the non-animated refresh icon
        inflater.inflate(R.menu.my_ab_menu, menu);
    
        if (isRefreshing) {
            //if we're refreshing, show the animation
            MenuItem item = menu.findItem(R.id.refreshMenuItem);
            item.setActionView(R.layout.action_bar_indeterminate_progress);
            ImageView iv = (ImageView) item.getActionView().findViewById(R.id.loadingImageView);
            ((AnimationDrawable) iv.getDrawable()).start();
        }
    }
    

    Start animation like so:

     public void refresh(){
            isRefreshing = true;
            inValidateOptionsMenu();
        }
    

    If you want the user to start the animation when he taps the refresh icon, do like this in onOptionsItemSelected():

    case R.id.refreshMenuItem:
        isRefreshing = true;
        item.setActionView(R.layout.action_bar_indeterminate_progress);
        ImageView iv = (ImageView) item.getActionView().findViewById(R.id.loadingImageView);
        ((AnimationDrawable) iv.getDrawable()).start();
        //...
    

    To stop the animation call:

    isRefreshing = false;
    invalidateOptionsMenu();
    

    This code is from a Fragment so you may have to tweak if for an Activity, but I think it communicates the basic idea.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When i used Actionbarsherlock public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home:
Here's a section of my code : @Override public boolean onOptionsItemSelected(MenuItem item) { switch
Having this method: public Boolean isCorrect() { return correct; } I can't write: @XmlType(propOrder
I have this code for update: public Boolean update() { try { data.put(ContactsContract.Groups.SHOULD_SYNC, true);
public class LinkedList { Object contents; LinkedList next = null; public boolean equals(Object item)
can someone explain me the difference between: onMenuItemSelected (int featureId, MenuItem item) http://developer.android.com/reference/android/app/Activity.html#onMenuItemSelected%28int,%20android.view.MenuItem%29 and
public boolean requestRouteToHost (int networkType, int hostAddress) This method in ConnectivityManager if used with
public class DocFilter extends FileFilter { public boolean accept(File f) { if (f.isDirectory()) {
I have the following method: @Override public boolean myMethod() { // do stuff }
I add this code in my activity public boolean onKeyDown(int keyCode, KeyEvent event) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.