Skip to content

Instantly share code, notes, and snippets.

@espritm
Last active March 28, 2017 09:29
Show Gist options
  • Save espritm/3643c1f280d5c1f1a50a0c94a552e3be to your computer and use it in GitHub Desktop.
Save espritm/3643c1f280d5c1f1a50a0c94a552e3be to your computer and use it in GitHub Desktop.
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