2

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?

Community
  • 1
  • 1
Nuno Marques
  • 321
  • 2
  • 7
  • what happens if you remove style="@style/Widget.AppCompat.ActionButton"? and why dont you call setActionView in onCreateOptionMenu()? – Harin Apr 14 '15 at 07:40
  • Nothing happens as iv_sync is not being shown. If I include it somewhere else for testing, removing the @style changes the view dimensions (I believe it removes padding). startSyncAnimation() is being called from syncList() which in turn is called from onCreateOptionsMenu(). I also call syncList() elsewhere. – Nuno Marques Apr 14 '15 at 09:30
  • Sorry the above is not correct, I call startSyncAnimation() from onOptionsItemSelected(). I don't call it from the onCreateOptionsMenu() because I only show iv_sync view (rotating icon) when I click the button. From my understanding, If i were to call setActionView when creating the menu, I would have to add a listener to iv_view to do the same as the button. – Nuno Marques Apr 14 '15 at 11:41

1 Answers1

0

Did you try moving the .setActionView() call to the end, after the animation is initialized?

    MenuItem item = getToolbar().getMenu().findItem(Menu.FIRST);

    Animation animation = new RotateAnimation(0.0f, 360.0f,
            Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF, 0.5f);
    animation.setDuration(700);
    animation.setRepeatCount(Animation.INFINITE);

    ImageView imageView = new ImageView(this);
    imageView.setImageDrawable(UIHelper.getIcon(this, MMEXIconFont.Icon.mmx_refresh));

    imageView.startAnimation(animation);
    item.setActionView(imageView);
Alen Siljak
  • 2,482
  • 2
  • 24
  • 29