0

Same question as here: Refresh menu item animation in ActionBarSherlock

Except that I would like to do this in API 11, using ActionBarActivity, and AppCompat v21 Material Design

refresh.xml

<?xml version="1.0" encoding="utf-8"?>

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:src="@drawable/ic_menu_refresh"
    android:background="#F00"/>

MyFragment.java

private View mRefresh;
private MenuItem mRefreshItem;
private boolean mIsRefreshing;
private Animation mRotate;

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mRotate = AnimationUtils.loadAnimation(getActivity(), R.anim.rotate);
        mRotate.setRepeatCount(Animation.INFINITE);
        mRotate.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                if(!mIsRefreshing){
                    mRefreshItem.getActionView().clearAnimation();
                    mRefreshItem.setActionView(null);
                }
            }
        });

        LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mRefresh = inflater.inflate(R.layout.refresh, null);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.menu, menu);
        mRefreshItem = menu.findItem(R.id.action_refresh);

        if(mIsRefreshing){
            mRefreshItem.setActionView(mRefresh);
            mRefresh.startAnimation(mRotate);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()){
            case R.id.action_refresh:
                if(!mIsRefreshing) {
                    mRefreshItem.setActionView(mRefresh);
                    mRefresh.startAnimation(mRotate);
                    startTask();
                }
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

The action view is not aligned with the static icon (red bg just so I can see the view):

menu

Community
  • 1
  • 1
tachyonflux
  • 20,103
  • 7
  • 48
  • 67

1 Answers1

0

Set the padding to 0:

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:src="@drawable/ic_menu_refresh"
    android:padding="0dp"
    style="@style/Widget.AppCompat.ActionButton"/>
tachyonflux
  • 20,103
  • 7
  • 48
  • 67