public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
return true;
case R.id.searchIcon:
return true;
case R.id.startRefresh:
refreshItem = item;
refresh();
return true;
case R.id.stopRefresh:
if (refreshItem != null && refreshItem.getActionView() != null) {
refreshItem.getActionView().clearAnimation();
refreshItem.setActionView(null);
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void refresh() {
if (FeedActivity.this != null) {
/*
* Attach a rotating ImageView to the refresh item as an ActionView
*/
LayoutInflater inflater = (LayoutInflater) FeedActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ImageView iv = (ImageView) inflater.inflate(
R.layout.refresh_action_view, null);
Animation rotation = AnimationUtils.loadAnimation(
FeedActivity.this, R.anim.clockwise_refresh);
rotation.setRepeatCount(Animation.INFINITE);
iv.startAnimation(rotation);
refreshItem.setActionView(iv);
}
}
Before Clicking:
After Clicking:
Here the icon is being animated(rotating).
Problem:
why is it shifting to the left?
once it shifts to the left, the icon becomes non clickable and strangely the device back button also doesn't work
EDIT:
In comments below this answer:
Jake Warton says if you are using a square and correct sized icon for the menu item, you wont get this weird behaviour, to someone who has the same problem.
But i am using a 32x32 image on a device which uses mdpi drawables. Which as stated there must work :(
Thank You
EDIT:
refresh_action_view.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/Widget.Sherlock.ActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_refresh" />
Custom Style i use in my app
<style name="My_solid_ActionBar" parent="@style/Widget.Sherlock.Light.ActionBar.Solid.Inverse">
<item name="background">@drawable/ab_solid_My</item>
<item name="backgroundStacked">@drawable/ab_stacked_solid_My</item>
<item name="backgroundSplit">@drawable/ab_bottom_solid_My</item>
<item name="progressBarStyle">@style/My_ProgressBar</item>
<item name="android:background">@drawable/ab_solid_My</item>
<item name="android:backgroundStacked">@drawable/ab_stacked_solid_My</item>
<item name="android:backgroundSplit">@drawable/ab_bottom_solid_My</item>
<item name="android:progressBarStyle">@style/My_ProgressBar</item>
</style>
I tried the exact same code you use and it works just fine for me. The only things that might be different are two things:
1) the refresh_action_view layout (here's mine for comparison):
2) The display options of your action bar (here's my styles.xml for comparison).
Can you share yours as well?
The issue is that you're not handling all menu inflation in
onCreateOptionsMenu()
. The basic logic for anActionBar
refresh animation I've seen used in apps with open source , for example Andlytics (and also used myself in projects), is to implement aboolean
flag inonCreateOptionsMenu()
to decide whether to show the refresh animation.You can implement it like this: When your
refresh()
method is called, it sets theboolean
isRefreshing
flag to true and callsinValidateOptionsMenu()
which 'behind the scene' callsonCreateOptionsMenu()
to start the animation:Inflate the menu in
onCreateOptionsMenu(...)
:Start animation like so:
If you want the user to start the animation when he taps the refresh icon, do like this in
onOptionsItemSelected()
:To stop the animation call:
This code is from a
Fragment
so you may have to tweak if for anActivity
, but I think it communicates the basic idea.