Search code examples
javaandroidmaterial-designbottomnavigationview

Deselect and Normalize default menu item selection


As you can see on the image below, I am having a hard time to figure out on how to deselect the first menu item since it is active by default on the BottomNavigationView.

I already tried everything but still the first menu is still raised.

Java Code.

public class MainActivity extends AppCompatActivity {  

 private BottomNavigationView bottomNavigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottomNavigation);
        bottomNavigationView.setBackground(null);
        bottomNavigationView.getMenu().setGroupCheckable(0, false, true);
        bottomNavigationView.setElevation(0);
        //bottomNavigationView.setSelectedItemId(R.id.menu2);

    }
}

Main XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/main_bottom_appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:fabCradleMargin="10dp"
        app:fabCradleRoundedCornerRadius="10dp"
        app:fabCradleVerticalOffset="10dp">

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottomNavigation"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginEnd="16dp"
            android:background="@android:color/transparent"
            app:menu="@menu/main_bottom_menu"
            />

    </com.google.android.material.bottomappbar.BottomAppBar>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        app:backgroundTint="@color/_Surface"
        app:tint="@color/_OnSurface"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/dashboard_24"
        app:layout_anchor="@+id/main_bottom_appbar"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

If you have any ideas on how to normalize the menu items on the BottomNavigationView, I will really appreciate the help.

Thanks.

enter image description here


Solution

  • Try to add this in your bottomNavigationView in main.xml

    app:labelVisibilityMode="unlabeled"
    

    So your view will be like this:

     <com.google.android.material.bottomnavigation.BottomNavigationView
                android:id="@+id/bottomNavigation"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginEnd="16dp"
                android:background="@android:color/transparent"
                app:menu="@menu/main_bottom_menu"
                app:labelVisibilityMode="unlabeled"
                />
    

    You can see more details about what changes you are allowed to make on view here: https://material.io/components/bottom-navigation/android#using-bottom-navigation