I'm trying to create a simple app interface using a navigation fragment, which swaps fragments in and out with a bottom navigation bar controlled by a navController
.
When I run the app it works as expected, however in the activity_main.xml file it doesn't show me the layout I see when I run the application. Is there any reason for this?
The code for the activity layout is below:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
app:menu="@menu/bottom_navigation_menu"
tools:visibility="visible" />
<!--android:background="#fff"/>-->
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
android:visibility="visible"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/navigation_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
FragmentContainerView
is a place holder that can show a fragment at a time when you do fragment transaction; so at design time, Android studio not sure which fragment that will be hosted first in this place holder, so it keeps it blank.
But if you want to show the start_destination fragment in the navigation_graph
you can use the tools:layout
attribute, and this only works in design time, not the run time.
Assuming the name of the fragment layout that you want to host first in the FragmentContainerView
is fragment_start_destination
tools:layout="@layout/fragment_start_destination"