0
Good afternoon, I have an app that has a refresh button on 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 to click, start an Animation on the button.
As a starting point, I took an example where you add a ProgressBar
in place of the button. Follows:
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 one worked perfectly, so I tried to change the 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? Thanks in advance!