Good afternoon, I have an application that has a refresh button in ActionBar:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:com.my.app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_refresh"
android:icon="@mipmap/ic_refresh"
android:title="@string/app_name"
com.my.app:showAsAction="always"
/>
</menu>
I would like that when clicking, start an Animation on the button. Home
As a starting point, I've taken an example where you add a ProgressBar
instead of the button. Following:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.action_refresh:
mRefreshAction = item;
mRefreshAction.setActionView(R.layout.menu_progress);
mRefreshAction.expandActionView();
break;
default:
break;
}
return true;
}
menu_progress.xml :
<?xml version="1.0" encoding="utf-8"?>
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/progressBar2"
android:indeterminate="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ProgressBar>
This worked perfectly, so I tried to swap ProgressBar
for an image and animate it:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.action_refresh:
mRefreshAction = item;
mRefreshAction.setActionView(R.layout.menu_progress);
mRefreshAction.expandActionView();
Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotate);
rotation.setRepeatCount(Animation.INFINITE);
mRefreshAction.getActionView().startAnimation(rotation);
break;
default:
break;
}
return true;
}
menu_progress.xml:
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/progressBar2"
android:src="@mipmap/ic_refresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
But the image is not displayed! Would anyone know how to animate an image in ActionBar ? Thank you!