I add Badge
on a button like this way.
I want to add Badge
on hamburger navigation
menu icon with this way but I don't know how.
I found a few solutions but none use BadgeDrawable
.
This is my code but the result is not what I want :
activity_main :
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawerLayout_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutDirection="rtl">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</FrameLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigationView_main"
android:layout_width="250dp"
android:layout_height="match_parent"
/>
</androidx.drawerlayout.widget.DrawerLayout>
MainActivity :
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NavigationView navigationView = findViewById(R.id.navigationView_main);
DrawerLayout drawerLayout = findViewById(R.id.drawerLayout_main);
Toolbar toolbar = findViewById(R.id.toolbar_main);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this , drawerLayout, toolbar , 0b0, 0);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
navigationView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
BadgeDrawable badgeDrawable = BadgeDrawable.create(MainActivity.this);
badgeDrawable.setVerticalOffset(25);
badgeDrawable.setHorizontalOffset(15);
BadgeUtils.attachBadgeDrawable(badgeDrawable, navigationView, null);
navigationView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
}
}
Result :
I want like this :
My question : Can I add Badge
on hamburger navigation
menu icon with BadgeDrawable
?
Implement DrawerArrowDrawable
class and override the draw(canvas: Canvas) method. For detailed example guide visit this link
https://android.pcsalt.com/add-badge-count-to-navigation-drawer-hamburger-icon-android/