0

I'm trying to apply a 'pulse' animation to bring attention to menu items within my Toolbar; however, I cannot find a way to apply the animation to the overflow icon if all menu items are hidden.

Using the code from this solution, I can get the ActionMenuView, but it doesn't have any children to apply an animation to.

//amv.getChildCount() returns 0.

Within my Fragment

@Override
public void onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    Toolbar toolbar = getToolbar();
    for (int i = 0; i < toolbar.getChildCount(); i++) {
    View view = toolbar.getChildAt(i);
    if(view.getClass().getSimpleName().equals("ActionMenuView")) {
        ActionMenuView amv = (ActionMenuView) view;
        for (int i1 = 0; i1 < amv.getChildCount(); i1++) {
            amv.getChildAt(i1).startAnimation((AnimationUtils.loadAnimation(getContext(),R.anim.pulse)));
        }
    }
}

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:res="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:id="@+id/tutorial"
    android:title="@string/tutorial"
    res:showAsAction="never"
    res:actionLayout="@layout/button_info_white"/>

<item
    android:id="@+id/social_filter"
    android:title="@string/social_filter"
    res:showAsAction="never"
    />

</menu>

Thanks for your assistance.

Community
  • 1
  • 1

1 Answers1

2

Since all of your items are not being displayed, I think you can do:

@Override
public void onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    Toolbar toolbar = getToolbar();
    for (int i = 0; i < toolbar.getChildCount(); i++) {
        View view = toolbar.getChildAt(i);
        if (view.getClass().getSimpleName().equals("ActionMenuView")) {
            view.startAnimation((AnimationUtils.loadAnimation(getContext(),R.anim.pulse)));
        }
    }
}

I made a very basic test and it works fine when all items are hide (and you has only the overflow icon).

Please, make additional tests to see if you don't have any side-effect.

guipivoto
  • 18,327
  • 9
  • 60
  • 75