Search code examples
javaandroidandroidxbottomnavigationview

androidx BottomNavigationView not showing up


I cannot get the bottom navigation to show up in androidx at all. I have even copied examples from other answers that are supposed to work, and they show up in the preview. activity.xlm:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/frameLayout"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/homeButton"
        android:enabled="true"
        android:icon="@android:drawable/ic_menu_agenda"
        android:title="a funny item"/>
    <item
        android:id="@+id/favoritesButton"
        android:enabled="true"
        android:icon="@android:drawable/ic_menu_info_details"
        android:title="favorites"/>
    <item
        android:id="@+id/ideaButton"
        android:enabled="true"
        android:icon="@android:drawable/ic_menu_agenda"
        android:title="ideas"/>
</menu>

Styles.xml:

 <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

activity code:

public class MainActivity extends AppCompatActivity {

    public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
    private AppBarConfiguration appBarConfiguration;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
}

What am I missing? Also why would it show up in the preview in android studio and not show when the app runs? Anyhelp is greatly appreciated


Solution

  • Yor forgot add setContentView(R.layout.activity_main); in your MainActivity

    public class MainActivity extends AppCompatActivity {
    
        public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
        private AppBarConfiguration appBarConfiguration;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    }