Creating Badge in Android Studio 2022.1.1 Patch 2 with Bottom Navigation Bar and etpack navigation component using Material Design 3

62 Views Asked by At

"Greetings everyone! Today, I am excited to share my exciting personal journey. While starting my app project, I ran into challenges while trying to create a Badge in Android Studio with Material Design 3. At first, it seemed like a complicated path, but In the end, I managed to overcome all the obstacles. I'm here to share with you how I did it!" examples: enter image description here

// This is the onStart method.
// It's used in the lifecycle of an activity to perform setups and preparations
// before the activity becomes visible to the user.
@Override
protected void onStart() {
    super.onStart();

    // Set up navigation and other UI elements here
    // This is done before the activity is fully visible to the user
    // to ensure everything is ready when it's displayed on the screen.
    
    // You can set up event listeners, start services, etc.
    
    // This is useful when you need certain actions to be performed before
    // the user can interact with the activity.
}

// This is the onCreate method, which is called after onStart.
// Here, the initial setup of the activity is done, like configuring the UI and preparing data.
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    // Other initial configurations can be done here.
}

complete code

`@Override
protected void onStart() {
    super.onStart();

    // Set up navigation controller for the bottom navigation view
    NavController navController = Navigation.findNavController(this, R.id.fragmentContainerView);
    NavigationUI.setupWithNavController(bottomNavigationView, navController, false);

    // Set up navigation controller for the navigation drawer
    NavController navControllerDrawer = Navigation.findNavController(this, R.id.fragmentContainerView);
    NavigationUI.setupWithNavController(navigationView, navControllerDrawer, false);

    // Handle item selection in the bottom navigation view
    bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @SuppressLint("NonConstantResourceId")
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.nav_inicio_op:
                    navController.navigate(R.id.nav_inicio_op);
                    break;
                case R.id.nav_observacion_op:
                    navController.navigate(R.id.nav_observacion_op);
                    break;
                case R.id.nav_trabajadores_op:
                    // Create and configure a badge for the "nav_trabajadores_op" item
                    BadgeDrawable badgeDrawable = bottomNavigationView.getOrCreateBadge(R.id.nav_trabajadores_op);
                    badgeDrawable.clearNumber();
                    badgeDrawable.setVisible(false);
                    // Navigate to the "nav_trabajadores_op" destination
                    navController.navigate(R.id.nav_trabajadores_op);
                    break;
                case R.id.nav_usuario_op:
                    navController.navigate(R.id.nav_usuario_op);
                    break;
            }

            return false;
        }
    });

    // Create and configure a counter view in the navigation drawer
    LayoutInflater li = LayoutInflater.from(getApplicationContext());
    tv_gallery_cunter = (TextView) li.inflate(R.layout.counter_gallery, null);
    navigationView.getMenu().findItem(R.id.nav_observacion_op).setActionView(tv_gallery_cunter);
    // Show the counter with a specific count number
    Show_Counter(gallery_count_number);
}

    // Crear y configurar una vista de contador en el menú desplegable de navegación (navigationView)
    LayoutInflater li = LayoutInflater.from(getApplicationContext());
    tv_gallery_cunter = (TextView) li.inflate(R.layout.counter_gallery, null);
    navigationView.getMenu().findItem(R.id.nav_observacion_op).setActionView(tv_gallery_cunter);
    // Mostrar el contador con un número específico
    Show_Counter(gallery_count_number);
}`
0

There are 0 best solutions below