Skip to content

Instantly share code, notes, and snippets.

@espritm
Last active Mar 28, 2017
Embed
What would you like to do?
private void M_navigationView_NavigationItemSelected(object sender, NavigationView.NavigationItemSelectedEventArgs e)
{
AddBadgeNotificationToMenuItem(e.MenuItem);
//...
}
private void AddBadgeNotificationToMenuItem(IMenuItem menuItem)
{
TextView textviewBadgeNotif = (TextView)MenuItemCompat.GetActionView(m_navigationView.Menu.FindItem(menuItem.ItemId));
//Red square with rounded corner and white text color
textviewBadgeNotif.SetBackgroundResource(Resource.Drawable.left_menus_itemBadge);
textviewBadgeNotif.SetTextColor(Color.White);
textviewBadgeNotif.Gravity = GravityFlags.Center;
//square size = 25 x 25 dp
FrameLayout.LayoutParams lparams = (FrameLayout.LayoutParams)textviewBadgeNotif.LayoutParameters;
lparams.Height = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, 25, Resources.DisplayMetrics);
lparams.Width = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, 25, Resources.DisplayMetrics);
textviewBadgeNotif.LayoutParameters = lparams;
//Show or hide badge notification
if (textviewBadgeNotif.Text != "")
{
textviewBadgeNotif.Text = "";
textviewBadgeNotif.Visibility = ViewStates.Gone;
}
else
{
textviewBadgeNotif.Text = "1";
textviewBadgeNotif.Visibility = ViewStates.Visible;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment