How to animate toolbar overflow menu icon

Is there a way to animate the default menu icon with 3 vertical items in the toolbar?

I use the toolbar as an action bar with standard code:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); 

and I also use the onCreateOptionsMenu method inside the action, where I inflate my menu.xml file but I don’t know how to get more control over the overflow icon, which is created automatically. What interests me most is how to refer to the menu icon. Therefore, I can animate it. I don't care about the type of animation. It could be a simple rotation animation.

+5
source share
1 answer

Ok, you play with View specifically ActionMenuView , so try this, copy the codes into your Activity

 //we declare our objects globally Toolbar tool; ActionMenuView amv; 

then override onPrepareOptionsMenu , what you decide on return is your choice

 @Override public boolean onPrepareOptionsMenu(Menu menu) { //to be safe you can check if children are greater than 1 amv = (ActionMenuView) tool.getChildAt(1);//hope you've met amv return true; } 

now this is the key part - whenever you want to animate “three vertical points” - (your overflow), you should check the visible children (for example, if you want) to actually forget that

 amv.getChildAt(amv.getChildCount()-1).startAnimation(AnimationUtils.loadAnimation( MainActivity.this,R.anim.abc_fade_in)); 

which gives you a basic fade animation - now you can slouch.

EDIT 1 :

In the above code, it was suggested that you didn’t add anything to the toolbar except to simply inflate the menu in onCreateOptionsMenu .

Suppose you have a sophisticated ToolBar use this sooner for your initialization

 @Override public boolean onPrepareOptionsMenu(Menu menu) { for(int i =0; i < tool.getChildCount(); ++i){ if(tool.getChildAt(i).getClass().getSimpleName().equals("ActionMenuView")){ amv = (ActionMenuView) tool.getChildAt(i); break; } } return true; } 

A lso where you call your amv View initialization can be in onCreateOptionsMenu or onPrepareOptionsMenu , I chose onPrepareOptionsMenu because I need readability

Hope this helps

+1
source

All Articles