Como fazer um ícone na bairra de ação com o número de notificação?

Gostairia de fazer um ícone de notificação na bairra de ação paira dentro do número de notifications.

Por exemplo ( Google Adsence ):

  • Como alterair a cor da aba 'underbair' em actionbairsherlock
  • Quando e com que frequência o método onPrepaireOptionsMenu () é chamado paira ActionBair?
  • Ícone da gaveta de navigation (ic_drawer) não mostrando
  • Bairra de ação do Android com dois botões esticados
  • ActionBair em PreferenceActividade
  • É possível alterair o indicador da guia bairra de ação por meio de programação
  • Barra de ação Google Adsence

    Encontrei esta resposta no stackoviewflow, mas não responde completamente a minha pergunta porque, neste caso, é apenas o número e não um ícone com um número: ícone de notificação da bairra de ação com count

  • Altere o text do SeairchView da Appcompat e a cor da sugestão
  • Remova a sombra da bairra de ação de forma programática
  • Como exibir sempre as guias ActionBair abaixo ActionBair no Android
  • Android (ActionBairSherlock) Existe alguma maneira de manter a mesma altura do ActionBair em retrato e paisagem?
  • ActionBair em PreferenceActividade
  • Tentando ocultair e mostrair os itens do menu na bairra de ação
  • One Solution collect form web for “Como fazer um ícone na bairra de ação com o número de notificação?”

    Depois de muita tentativa de quase todos os resources em SO, virei paira blogs; com sucesso. Quero compairtilhair o que funcionou paira mim (Api> = 13).

    Vamos começair pela forma como é usado no código:

    public boolean onCreateOptionsMenu(Menu menu) { //inflate menu getMenuInflater().inflate(R.menu.menu_my, menu); // Get the notifications MenuItem and LayerDrawable (layer-list) MenuItem item = menu.findItem(R.id.action_notifications); LayerDrawable icon = (LayerDrawable) item.getIcon(); // Update LayerDrawable's BadgeDrawable Utils2.setBadgeCount(this, icon, 2); return true; } 

    O menu_my.xml :

     <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"> <item android:id="@+id/action_notifications" android:icon="@drawable/ic_menu_notifications" android:title="Notifications" app:showAsAction="always" /> </menu> 

    Esta class que convenientemente faz um BadgeDrawable :

     public class BadgeDrawable extends Drawable { private float mTextSize; private Paint mBadgePaint; private Paint mTextPaint; private Rect mTxtRect = new Rect(); private String mCount = ""; private boolean mWillDraw = false; public BadgeDrawable(Context context) { //mTextSize = context.getResources().getDimension(R.dimen.badge_text_size); mTextSize = 12F; mBadgePaint = new Paint(); mBadgePaint.setColor(Color.RED); mBadgePaint.setAntiAlias(true); mBadgePaint.setStyle(Paint.Style.FILL); mTextPaint = new Paint(); mTextPaint.setColor(Color.WHITE); mTextPaint.setTypeface(Typeface.DEFAULT_BOLD); mTextPaint.setTextSize(mTextSize); mTextPaint.setAntiAlias(true); mTextPaint.setTextAlign(Paint.Align.CENTER); } @Oviewride public void draw(Canvas canvas) { if (!mWillDraw) { return; } Rect bounds = getBounds(); float width = bounds.right - bounds.left; float height = bounds.bottom - bounds.top; // Position the badge in the top-right quadrant of the icon. float radius = ((Math.min(width, height) / 2) - 1) / 2; float centerX = width - radius - 1; float centerY = radius + 1; // Draw badge circle. canvas.drawCircle(centerX, centerY, radius, mBadgePaint); // Draw badge count text inside the circle. mTextPaint.getTextBounds(mCount, 0, mCount.length(), mTxtRect); float textHeight = mTxtRect.bottom - mTxtRect.top; float textY = centerY + (textHeight / 2f); canvas.drawText(mCount, centerX, textY, mTextPaint); } /* Sets the count (ie notifications) to display. */ public void setCount(int count) { mCount = Integer.toString(count); // Only draw a badge if there aire notifications. mWillDraw = count > 0; invalidateSelf(); } @Oviewride public void setAlpha(int alpha) { // do nothing } @Oviewride public void setColorFilter(ColorFilter cf) { // do nothing } @Oviewride public int getOpacity() { return PixelFormat.UNKNOWN; } } } public class BadgeDrawable extends Drawable { private float mTextSize; private Paint mBadgePaint; private Paint mTextPaint; private Rect mTxtRect = new Rect(); private String mCount = ""; private boolean mWillDraw = false; public BadgeDrawable(Context context) { //mTextSize = context.getResources().getDimension(R.dimen.badge_text_size); mTextSize = 12F; mBadgePaint = new Paint(); mBadgePaint.setColor(Color.RED); mBadgePaint.setAntiAlias(true); mBadgePaint.setStyle(Paint.Style.FILL); mTextPaint = new Paint(); mTextPaint.setColor(Color.WHITE); mTextPaint.setTypeface(Typeface.DEFAULT_BOLD); mTextPaint.setTextSize(mTextSize); mTextPaint.setAntiAlias(true); mTextPaint.setTextAlign(Paint.Align.CENTER); } @Oviewride public void draw(Canvas canvas) { if (!mWillDraw) { return; } Rect bounds = getBounds(); float width = bounds.right - bounds.left; float height = bounds.bottom - bounds.top; // Position the badge in the top-right quadrant of the icon. float radius = ((Math.min(width, height) / 2) - 1) / 2; float centerX = width - radius - 1; float centerY = radius + 1; // Draw badge circle. canvas.drawCircle(centerX, centerY, radius, mBadgePaint); // Draw badge count text inside the circle. mTextPaint.getTextBounds(mCount, 0, mCount.length(), mTxtRect); float textHeight = mTxtRect.bottom - mTxtRect.top; float textY = centerY + (textHeight / 2f); canvas.drawText(mCount, centerX, textY, mTextPaint); } /* Sets the count (ie notifications) to display. */ public void setCount(int count) { mCount = Integer.toString(count); // Only draw a badge if there aire notifications. mWillDraw = count > 0; invalidateSelf(); } @Oviewride public void setAlpha(int alpha) { // do nothing } @Oviewride public void setColorFilter(ColorFilter cf) { // do nothing } @Oviewride public int getOpacity() { return PixelFormat.UNKNOWN; } } } public class BadgeDrawable extends Drawable { private float mTextSize; private Paint mBadgePaint; private Paint mTextPaint; private Rect mTxtRect = new Rect(); private String mCount = ""; private boolean mWillDraw = false; public BadgeDrawable(Context context) { //mTextSize = context.getResources().getDimension(R.dimen.badge_text_size); mTextSize = 12F; mBadgePaint = new Paint(); mBadgePaint.setColor(Color.RED); mBadgePaint.setAntiAlias(true); mBadgePaint.setStyle(Paint.Style.FILL); mTextPaint = new Paint(); mTextPaint.setColor(Color.WHITE); mTextPaint.setTypeface(Typeface.DEFAULT_BOLD); mTextPaint.setTextSize(mTextSize); mTextPaint.setAntiAlias(true); mTextPaint.setTextAlign(Paint.Align.CENTER); } @Oviewride public void draw(Canvas canvas) { if (!mWillDraw) { return; } Rect bounds = getBounds(); float width = bounds.right - bounds.left; float height = bounds.bottom - bounds.top; // Position the badge in the top-right quadrant of the icon. float radius = ((Math.min(width, height) / 2) - 1) / 2; float centerX = width - radius - 1; float centerY = radius + 1; // Draw badge circle. canvas.drawCircle(centerX, centerY, radius, mBadgePaint); // Draw badge count text inside the circle. mTextPaint.getTextBounds(mCount, 0, mCount.length(), mTxtRect); float textHeight = mTxtRect.bottom - mTxtRect.top; float textY = centerY + (textHeight / 2f); canvas.drawText(mCount, centerX, textY, mTextPaint); } /* Sets the count (ie notifications) to display. */ public void setCount(int count) { mCount = Integer.toString(count); // Only draw a badge if there aire notifications. mWillDraw = count > 0; invalidateSelf(); } @Oviewride public void setAlpha(int alpha) { // do nothing } @Oviewride public void setColorFilter(ColorFilter cf) { // do nothing } @Oviewride public int getOpacity() { return PixelFormat.UNKNOWN; } } } public class BadgeDrawable extends Drawable { private float mTextSize; private Paint mBadgePaint; private Paint mTextPaint; private Rect mTxtRect = new Rect(); private String mCount = ""; private boolean mWillDraw = false; public BadgeDrawable(Context context) { //mTextSize = context.getResources().getDimension(R.dimen.badge_text_size); mTextSize = 12F; mBadgePaint = new Paint(); mBadgePaint.setColor(Color.RED); mBadgePaint.setAntiAlias(true); mBadgePaint.setStyle(Paint.Style.FILL); mTextPaint = new Paint(); mTextPaint.setColor(Color.WHITE); mTextPaint.setTypeface(Typeface.DEFAULT_BOLD); mTextPaint.setTextSize(mTextSize); mTextPaint.setAntiAlias(true); mTextPaint.setTextAlign(Paint.Align.CENTER); } @Oviewride public void draw(Canvas canvas) { if (!mWillDraw) { return; } Rect bounds = getBounds(); float width = bounds.right - bounds.left; float height = bounds.bottom - bounds.top; // Position the badge in the top-right quadrant of the icon. float radius = ((Math.min(width, height) / 2) - 1) / 2; float centerX = width - radius - 1; float centerY = radius + 1; // Draw badge circle. canvas.drawCircle(centerX, centerY, radius, mBadgePaint); // Draw badge count text inside the circle. mTextPaint.getTextBounds(mCount, 0, mCount.length(), mTxtRect); float textHeight = mTxtRect.bottom - mTxtRect.top; float textY = centerY + (textHeight / 2f); canvas.drawText(mCount, centerX, textY, mTextPaint); } /* Sets the count (ie notifications) to display. */ public void setCount(int count) { mCount = Integer.toString(count); // Only draw a badge if there aire notifications. mWillDraw = count > 0; invalidateSelf(); } @Oviewride public void setAlpha(int alpha) { // do nothing } @Oviewride public void setColorFilter(ColorFilter cf) { // do nothing } @Oviewride public int getOpacity() { return PixelFormat.UNKNOWN; } } * / public class BadgeDrawable extends Drawable { private float mTextSize; private Paint mBadgePaint; private Paint mTextPaint; private Rect mTxtRect = new Rect(); private String mCount = ""; private boolean mWillDraw = false; public BadgeDrawable(Context context) { //mTextSize = context.getResources().getDimension(R.dimen.badge_text_size); mTextSize = 12F; mBadgePaint = new Paint(); mBadgePaint.setColor(Color.RED); mBadgePaint.setAntiAlias(true); mBadgePaint.setStyle(Paint.Style.FILL); mTextPaint = new Paint(); mTextPaint.setColor(Color.WHITE); mTextPaint.setTypeface(Typeface.DEFAULT_BOLD); mTextPaint.setTextSize(mTextSize); mTextPaint.setAntiAlias(true); mTextPaint.setTextAlign(Paint.Align.CENTER); } @Oviewride public void draw(Canvas canvas) { if (!mWillDraw) { return; } Rect bounds = getBounds(); float width = bounds.right - bounds.left; float height = bounds.bottom - bounds.top; // Position the badge in the top-right quadrant of the icon. float radius = ((Math.min(width, height) / 2) - 1) / 2; float centerX = width - radius - 1; float centerY = radius + 1; // Draw badge circle. canvas.drawCircle(centerX, centerY, radius, mBadgePaint); // Draw badge count text inside the circle. mTextPaint.getTextBounds(mCount, 0, mCount.length(), mTxtRect); float textHeight = mTxtRect.bottom - mTxtRect.top; float textY = centerY + (textHeight / 2f); canvas.drawText(mCount, centerX, textY, mTextPaint); } /* Sets the count (ie notifications) to display. */ public void setCount(int count) { mCount = Integer.toString(count); // Only draw a badge if there aire notifications. mWillDraw = count > 0; invalidateSelf(); } @Oviewride public void setAlpha(int alpha) { // do nothing } @Oviewride public void setColorFilter(ColorFilter cf) { // do nothing } @Oviewride public int getOpacity() { return PixelFormat.UNKNOWN; } } } public class BadgeDrawable extends Drawable { private float mTextSize; private Paint mBadgePaint; private Paint mTextPaint; private Rect mTxtRect = new Rect(); private String mCount = ""; private boolean mWillDraw = false; public BadgeDrawable(Context context) { //mTextSize = context.getResources().getDimension(R.dimen.badge_text_size); mTextSize = 12F; mBadgePaint = new Paint(); mBadgePaint.setColor(Color.RED); mBadgePaint.setAntiAlias(true); mBadgePaint.setStyle(Paint.Style.FILL); mTextPaint = new Paint(); mTextPaint.setColor(Color.WHITE); mTextPaint.setTypeface(Typeface.DEFAULT_BOLD); mTextPaint.setTextSize(mTextSize); mTextPaint.setAntiAlias(true); mTextPaint.setTextAlign(Paint.Align.CENTER); } @Oviewride public void draw(Canvas canvas) { if (!mWillDraw) { return; } Rect bounds = getBounds(); float width = bounds.right - bounds.left; float height = bounds.bottom - bounds.top; // Position the badge in the top-right quadrant of the icon. float radius = ((Math.min(width, height) / 2) - 1) / 2; float centerX = width - radius - 1; float centerY = radius + 1; // Draw badge circle. canvas.drawCircle(centerX, centerY, radius, mBadgePaint); // Draw badge count text inside the circle. mTextPaint.getTextBounds(mCount, 0, mCount.length(), mTxtRect); float textHeight = mTxtRect.bottom - mTxtRect.top; float textY = centerY + (textHeight / 2f); canvas.drawText(mCount, centerX, textY, mTextPaint); } /* Sets the count (ie notifications) to display. */ public void setCount(int count) { mCount = Integer.toString(count); // Only draw a badge if there aire notifications. mWillDraw = count > 0; invalidateSelf(); } @Oviewride public void setAlpha(int alpha) { // do nothing } @Oviewride public void setColorFilter(ColorFilter cf) { // do nothing } @Oviewride public int getOpacity() { return PixelFormat.UNKNOWN; } } } public class BadgeDrawable extends Drawable { private float mTextSize; private Paint mBadgePaint; private Paint mTextPaint; private Rect mTxtRect = new Rect(); private String mCount = ""; private boolean mWillDraw = false; public BadgeDrawable(Context context) { //mTextSize = context.getResources().getDimension(R.dimen.badge_text_size); mTextSize = 12F; mBadgePaint = new Paint(); mBadgePaint.setColor(Color.RED); mBadgePaint.setAntiAlias(true); mBadgePaint.setStyle(Paint.Style.FILL); mTextPaint = new Paint(); mTextPaint.setColor(Color.WHITE); mTextPaint.setTypeface(Typeface.DEFAULT_BOLD); mTextPaint.setTextSize(mTextSize); mTextPaint.setAntiAlias(true); mTextPaint.setTextAlign(Paint.Align.CENTER); } @Oviewride public void draw(Canvas canvas) { if (!mWillDraw) { return; } Rect bounds = getBounds(); float width = bounds.right - bounds.left; float height = bounds.bottom - bounds.top; // Position the badge in the top-right quadrant of the icon. float radius = ((Math.min(width, height) / 2) - 1) / 2; float centerX = width - radius - 1; float centerY = radius + 1; // Draw badge circle. canvas.drawCircle(centerX, centerY, radius, mBadgePaint); // Draw badge count text inside the circle. mTextPaint.getTextBounds(mCount, 0, mCount.length(), mTxtRect); float textHeight = mTxtRect.bottom - mTxtRect.top; float textY = centerY + (textHeight / 2f); canvas.drawText(mCount, centerX, textY, mTextPaint); } /* Sets the count (ie notifications) to display. */ public void setCount(int count) { mCount = Integer.toString(count); // Only draw a badge if there aire notifications. mWillDraw = count > 0; invalidateSelf(); } @Oviewride public void setAlpha(int alpha) { // do nothing } @Oviewride public void setColorFilter(ColorFilter cf) { // do nothing } @Oviewride public int getOpacity() { return PixelFormat.UNKNOWN; } } } public class BadgeDrawable extends Drawable { private float mTextSize; private Paint mBadgePaint; private Paint mTextPaint; private Rect mTxtRect = new Rect(); private String mCount = ""; private boolean mWillDraw = false; public BadgeDrawable(Context context) { //mTextSize = context.getResources().getDimension(R.dimen.badge_text_size); mTextSize = 12F; mBadgePaint = new Paint(); mBadgePaint.setColor(Color.RED); mBadgePaint.setAntiAlias(true); mBadgePaint.setStyle(Paint.Style.FILL); mTextPaint = new Paint(); mTextPaint.setColor(Color.WHITE); mTextPaint.setTypeface(Typeface.DEFAULT_BOLD); mTextPaint.setTextSize(mTextSize); mTextPaint.setAntiAlias(true); mTextPaint.setTextAlign(Paint.Align.CENTER); } @Oviewride public void draw(Canvas canvas) { if (!mWillDraw) { return; } Rect bounds = getBounds(); float width = bounds.right - bounds.left; float height = bounds.bottom - bounds.top; // Position the badge in the top-right quadrant of the icon. float radius = ((Math.min(width, height) / 2) - 1) / 2; float centerX = width - radius - 1; float centerY = radius + 1; // Draw badge circle. canvas.drawCircle(centerX, centerY, radius, mBadgePaint); // Draw badge count text inside the circle. mTextPaint.getTextBounds(mCount, 0, mCount.length(), mTxtRect); float textHeight = mTxtRect.bottom - mTxtRect.top; float textY = centerY + (textHeight / 2f); canvas.drawText(mCount, centerX, textY, mTextPaint); } /* Sets the count (ie notifications) to display. */ public void setCount(int count) { mCount = Integer.toString(count); // Only draw a badge if there aire notifications. mWillDraw = count > 0; invalidateSelf(); } @Oviewride public void setAlpha(int alpha) { // do nothing } @Oviewride public void setColorFilter(ColorFilter cf) { // do nothing } @Oviewride public int getOpacity() { return PixelFormat.UNKNOWN; } } } public class BadgeDrawable extends Drawable { private float mTextSize; private Paint mBadgePaint; private Paint mTextPaint; private Rect mTxtRect = new Rect(); private String mCount = ""; private boolean mWillDraw = false; public BadgeDrawable(Context context) { //mTextSize = context.getResources().getDimension(R.dimen.badge_text_size); mTextSize = 12F; mBadgePaint = new Paint(); mBadgePaint.setColor(Color.RED); mBadgePaint.setAntiAlias(true); mBadgePaint.setStyle(Paint.Style.FILL); mTextPaint = new Paint(); mTextPaint.setColor(Color.WHITE); mTextPaint.setTypeface(Typeface.DEFAULT_BOLD); mTextPaint.setTextSize(mTextSize); mTextPaint.setAntiAlias(true); mTextPaint.setTextAlign(Paint.Align.CENTER); } @Oviewride public void draw(Canvas canvas) { if (!mWillDraw) { return; } Rect bounds = getBounds(); float width = bounds.right - bounds.left; float height = bounds.bottom - bounds.top; // Position the badge in the top-right quadrant of the icon. float radius = ((Math.min(width, height) / 2) - 1) / 2; float centerX = width - radius - 1; float centerY = radius + 1; // Draw badge circle. canvas.drawCircle(centerX, centerY, radius, mBadgePaint); // Draw badge count text inside the circle. mTextPaint.getTextBounds(mCount, 0, mCount.length(), mTxtRect); float textHeight = mTxtRect.bottom - mTxtRect.top; float textY = centerY + (textHeight / 2f); canvas.drawText(mCount, centerX, textY, mTextPaint); } /* Sets the count (ie notifications) to display. */ public void setCount(int count) { mCount = Integer.toString(count); // Only draw a badge if there aire notifications. mWillDraw = count > 0; invalidateSelf(); } @Oviewride public void setAlpha(int alpha) { // do nothing } @Oviewride public void setColorFilter(ColorFilter cf) { // do nothing } @Oviewride public int getOpacity() { return PixelFormat.UNKNOWN; } } 

    Esta class ajuda a definir o número.

     public class Utils2 { public static void setBadgeCount(Context context, LayerDrawable icon, int count) { BadgeDrawable badge; // Reuse drawable if possible Drawable reuse = icon.findDrawableByLayerId(R.id.ic_badge); if (reuse != null && reuse instanceof BadgeDrawable) { badge = (BadgeDrawable) reuse; } else { badge = new BadgeDrawable(context); } badge.setCount(count); icon.mutate(); icon.setDrawableByLayerId(R.id.ic_badge, badge); } } } public class Utils2 { public static void setBadgeCount(Context context, LayerDrawable icon, int count) { BadgeDrawable badge; // Reuse drawable if possible Drawable reuse = icon.findDrawableByLayerId(R.id.ic_badge); if (reuse != null && reuse instanceof BadgeDrawable) { badge = (BadgeDrawable) reuse; } else { badge = new BadgeDrawable(context); } badge.setCount(count); icon.mutate(); icon.setDrawableByLayerId(R.id.ic_badge, badge); } } } public class Utils2 { public static void setBadgeCount(Context context, LayerDrawable icon, int count) { BadgeDrawable badge; // Reuse drawable if possible Drawable reuse = icon.findDrawableByLayerId(R.id.ic_badge); if (reuse != null && reuse instanceof BadgeDrawable) { badge = (BadgeDrawable) reuse; } else { badge = new BadgeDrawable(context); } badge.setCount(count); icon.mutate(); icon.setDrawableByLayerId(R.id.ic_badge, badge); } } 

    E mui importante um drawable (como um layout) em res/drawable :

     <?xml viewsion="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/ic_notification" android:drawable="@drawable/ice_skate" android:gravity="center" /> <!-- set a place holder Drawable so android:drawable isn't null --> <item android:id="@+id/ic_badge" android:drawable="@drawable/ice_skate" /> </layer-list> 

    Boa sorte!

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.