Skip to content

Instantly share code, notes, and snippets.

@nguyenhuy
Last active January 2, 2016 16:49
Show Gist options
  • Save nguyenhuy/8332928 to your computer and use it in GitHub Desktop.
Save nguyenhuy/8332928 to your computer and use it in GitHub Desktop.
Show a badge on top of an ABS's MenuItem, using BadgeView from https://github.com/jgilfelt/android-viewbadger.
private BadgeView mBadgeView;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.badge_demo, menu);
MenuItem menuItem = menu.findItem(R.id.item_id);
ImageButton iconView = new ImageButton(this, null,
com.actionbarsherlock.R.style.Widget_Sherlock_ActionButton);
iconView.setImageDrawable(menuItem.getIcon());
iconView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// The thing is clicked, do something useful.
}
});
// The badge view requires target view (iconView in this case)
// to have a ViewGroup parent
LinearLayout container = new LinearLayout(this);
container.setLayoutParams(new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
container.addView(iconView);
mBadgeView = new BadgeView(this, iconView);
mBadgeView.setText("10");
mBadgeView.show();
menuItem.setActionView(container);
return true;
}
@techartist
Copy link

@SSSANTON
Copy link

Great! It's work at action bar now.Thanks!

The icon seems bigger than normal.
I need to scale it smaller.
container.setScaleX(new Float(0.7));
container.setScaleY(new Float(0.7));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment