Chrispix
Chrispix

Reputation: 18231

Custom View in place of MenuItem.Icon() ActionBarSherlock

Trying to do some customization to my Actionbar w/ Actionbar Sherlock..

I have an existing custom View which over-rides on Draw, and updates itself with the GPS status based on Accuracy.

I want it as the right most menu option available to users. (So I believe customView is out).

I have tried to implement this as a MenuItem on the Action bar, which I was able to do successfully, by extending Drawable instead of extending a View (or ImageView).

My issue seems to be w/ extending drawable, I can't invalidate my view to refresh, when a GPS accuracy changes. I can call invalidateSelf, and I have a Callback listener enabled where invalidate(Drawable who) is passed in, but if I try to set the who as the updated image, nothing happens..

If I tap the icon, it refreshes & updates..

Wondering if there is a better solution than extending drawable, or if I extend drawable, how can I make it dynamically update w/out user input.

Upvotes: 0

Views: 285

Answers (1)

Luke Wallace
Luke Wallace

Reputation: 1344

If there are only a few distinct states to show (perhaps 4 or 5 icons that could be bundled with your app), I would suggest just updating your menu options dynamically during onPrepareOptionsMenu().

public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem gpsStatus = menu.findItem(R.id.menu_gps);

    // figure out which icon to show here
    // int drawableId = R.drawable.ic_gps_accurate

    gpsStatus.setIcon(drawableId);
}

To get this code to trigger, simply call invalidateOptionsMenu(); You'll probably want to save the state so it's quicker & easier for your onPrepareOptionsMenu() to figure out the right icon.

private void onGPSStatusUpdated(int newStatus) {
    mGPSStatus = newStatus;
    invalidateOptionsMenu();
}

Upvotes: 1

Related Questions