I basically followed this answer in order to animate the refresh menu button, the only difference being I'm using AppCompat v7.22 instead of ActionBarSherlock.
I have a toolbar in my app defined as (layout/toolbar.xml)
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
My menu contains an item corresponding to a refresh/sync button (menu/menu_list.xml):
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
// ...
<item android:id="@+id/sync"
android:title="Sincronizar"
app:showAsAction="always"
android:icon="@drawable/ic_action_refresh" />
// ...
</menu>
And the view which will be animated (layout/iv_sync.xml):
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/Widget.AppCompat.ActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_action_refresh" />
When the sync button is pressed, I set the above view to the item's action view and animate it (ListActivity.java):
private void startSyncAnimation() {
MenuItem menuItem = myMenu.findItem(R.id.sync);
ImageView iv = (ImageView) getLayoutInflater().inflate(R.layout.iv_sync, null);
menuItem.setActionView(iv); // this does nothing to the UI!
Animation rotate = AnimationUtils.loadAnimation(this, R.anim.rotate);
rotate.setRepeatCount(Animation.INFINITE);
iv.startAnimation(rotate);
}
I won't show the rotate animation code as I checked that it works. The problem is apparently the line menuItem.setActionView(iv)
, which, although it is setting the action view (menuItem.getActionView() would return correctly), is doing nothing to the UI.
I'm targeting API 22 and min is API 16. Any ideas on what may be the issue?