Last active
January 2, 2016 16:49
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
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
I am having a problem with this solution:
http://stackoverflow.com/questions/26660331/adding-setshowasactionflags-to-menuitem-is-causing-indexoutofboundsexception
Any ideas?