I follow the answer from this question: link
But I get this on my app:
As you can see on the ActionBar's right side there are two refresh icons (they were actually rotating when I took the screenshot). But I just want one to be there. I'm quite sure the problem is one of them is the item from the Menu XML file and the other is the ImageView from the actionbar_inderterminate_progress.xml
file.
Here is my menu.xml file:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.mypackage.android.design.appdesgin.MainActivity" >
<item android:id="@+id/action_search"
android:title="Search"
android:icon="@drawable/ic_action_search"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView"
android:visible="false" />
<item android:id="@+id/action_create_local_backup"
android:title="Search"
android:icon="@drawable/ic_action_new"
android:showAsAction="always"
android:visible="false" />
<item
android:id="@+id/action_refresh"
android:icon="@drawable/ic_menu_refresh"
android:showAsAction="always"
android:title="Refresh">
</item>
<TextView
android:id="@+id/status_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"/>
And here is my actionbar_inderterminate_progress.xml
:
<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/indeterminate_progress"
style="@android:style/Widget.ActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/app_name"
android:src="@drawable/ic_menu_refresh" />
My animation XML file:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000"
android:interpolator="@android:anim/accelerate_decelerate_interpolator" />
And my java code:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh:
refreshItem = item;
refresh();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void refresh() {
/* Attach a rotating ImageView to the refresh item as an ActionView */
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ImageView imageView = (ImageView) inflater.inflate(R.layout.refresh_button, null);
Animation rotation = AnimationUtils.loadAnimation(getActivity(), R.animator.rotate_refresh);
rotation.setRepeatCount(Animation.INFINITE);
imageView.startAnimation(rotation);
refreshItem.setActionView(imageView);
}
Can anyone point me where and what am I doing wrong to make it only display one animated refresh button?