7

I have a menu item with an icon (imagine for example a clock with a single lancet), I would like to make its icon rotate indefinitely.

How could I manage this effect? Thank you.

4
  • You can use Indeterminate progress bar : developer.android.com/reference/android/widget/… Commented Mar 3, 2015 at 19:59
  • Can I use it also to make a custom icon rotate? Commented Mar 3, 2015 at 20:00
  • Ohh you want to rotate custom icon... I think you can... let me check if I get something for you Commented Mar 3, 2015 at 20:02
  • I think this will work : stackoverflow.com/questions/9162481/… not sure though so there is no harm in trying. Commented Mar 3, 2015 at 20:05

2 Answers 2

24

Add a file res/layout/iv_refresh.xml (replace ic_launcher with your custom icon):

<?xml version="1.0" encoding="utf-8"?> <ImageView xmlns:android="http://schemas.android.com/apk/res/android" style="@android:style/Widget.ActionButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/app_name" android:src="@drawable/ic_launcher" /> 

Add a file res/anim/rotate_refresh.xml:

<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:fromDegrees="0" android:interpolator="@android:anim/linear_interpolator" android:pivotX="50%" android:pivotY="50%" android:toDegrees="360"> </rotate> 

Finally in your java code you can start the animation like this:

LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); ImageView iv = (ImageView)inflater.inflate(R.layout.iv_refresh, null); Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotate_refresh); rotation.setRepeatCount(Animation.INFINITE); iv.startAnimation(rotation); menu.findItem(R.id.my_menu_item_id).setActionView(iv); 
Sign up to request clarification or add additional context in comments.

13 Comments

But how to set this for menu item?
Get your view and assign the animation
I tried with this: ((View)menu.findItem(R.id.my_menu_item_id)).startAnimation(mAnimation); but I got this: java.lang.ClassCastException: android.support.v7.internal.view.menu.m cannot be cast to android.view.View
Could you provide an example where you show how to do this?
|
2

the best way is here:

public class HomeActivity extends AppCompatActivity { public static ActionMenuItemView btsync; public static RotateAnimation rotateAnimation; @Override protected void onCreate(Bundle savedInstanceState) { rotateAnimation = new RotateAnimation(360, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); rotateAnimation.setDuration((long) 2*500); rotateAnimation.setRepeatCount(Animation.INFINITE); 

and then:

private void sync() { btsync = this.findViewById(R.id.action_sync); //remember that u cant access this view at onCreate() or onStart() or onResume() or onPostResume() or onPostCreate() or onCreateOptionsMenu() or onPrepareOptionsMenu() if (isSyncServiceRunning(HomeActivity.this)) { showConfirmStopDialog(); } else { if (btsync != null) { btsync.startAnimation(rotateAnimation); } Context context = getApplicationContext(); context.startService(new Intent(context, SyncService.class)); } } 

Remember that u cant access "btsync = this.findViewById(R.id.action_sync);" at onCreate() or onStart() or onResume() or onPostResume() or onPostCreate() or onCreateOptionsMenu() or onPrepareOptionsMenu() if u want get it just after activity start put it in a postdelayed:

public static void refreshSync(Activity context) { Handler handler = new Handler(Looper.getMainLooper()); handler.postDelayed(new Runnable() { public void run() { btsync = context.findViewById(R.id.action_sync); if (btsync != null && isSyncServiceRunning(context)) { btsync.startAnimation(rotateAnimation); } else if (btsync != null) { btsync.clearAnimation(); } } }, 1000); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.