3

I have toolbar with a view pager and two tabs which are fragments in my application. When the user swipes between the two, a different menu item displays in the toolbar. I am able to do this with no problem. However, I want to get a fade animation between the two menu items when they are switching.

How I achieve the switch between the menu items follows.

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.dashboard, menu);
    this.menu = menu; //get reference to menu

    return super.onCreateOptionsMenu(menu);
}

I get a reference to the menu onCreateOptionsMenu() method. Then using an update method I pass a value when the tabs are switched.

private void updateMenuTitles(int value)
{
    if(value == 0)
    {
        MenuItem sort = menu.findItem(R.id.sort_menu);
        sort.setVisible(true);

        MenuItem dateRange = menu.findItem(R.id.date_range);
        dateRange.setVisible(false);
    }
    else if(value == 1)
    {
        MenuItem dateRange = menu.findItem(R.id.date_range);
        dateRange.setVisible(true);

        MenuItem sort = menu.findItem(R.id.sort_menu);
        sort.setVisible(false);
    }
}

XML for the menu.

<item
    android:id="@+id/date_range"
    android:title="@string/sort_options"
    android:icon="@drawable/ic_date_range_white_24dp"
    android:orderInCategory="2"
    android:visible="false"
    app:showAsAction="ifRoom">

</item>

<item
    android:id="@+id/sort_menu"
    android:title="@string/sort_options"
    android:icon="@drawable/ic_sort_white_24dp"
    android:orderInCategory="2"
    app:showAsAction="ifRoom">

    <menu>
        <item
            android:id="@+id/sortName"
            android:title="@string/sort_name"
            app:showAsAction="never"
            />

        <item
            android:id="@+id/sortSize"
            android:title="@string/sort_size"
            app:showAsAction="never"
            />

        <item
            android:id="@+id/sortDate"
            android:title="@string/sort_date"
            app:showAsAction="never"
            />

        <item
            android:id="@+id/sortLastUsed"
            android:title="@string/sort_usage"
            app:showAsAction="never"
            />
    </menu>

</item>

The above works fine. The problem arises when I want to add an animation between sort menu item and the dateRange menu item, when switching tabs.

I have search and tried to use the following stackoverflow posts with no success.

Changing view on Menu Item in Action Bar

getActionView() of my MenuItem return null

Animated Icon for ActionItem

Add ActionBar Items with a fade animation

Using the above information I tried setting an android:actionViewClass which returns null when I try to fetch it. Then I tried app:actionViewClass which makes my menu item disappear.

I tried to setActionView in the code instead of XML, then try to animate that with no luck.

I also tried to use android(app):actionLayout. Make an ImageView layout, but that makes my menu item unclickable and I was still unable to animate the fade.

Community
  • 1
  • 1
exc414
  • 45
  • 1
  • 8

0 Answers0