In order to be able to see the content of a certain view inside a fragment when previewing a layout in Android Studio, it is possible to do this:
<MainLayoutView 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">
<fragment
...
tools:layout="@layout/my_fragment_view"/>
</MainLayoutView>
However, if you switch from fragment
to androidx.fragment.app.FragmentContainerView
, this handy tool stops working and the fragment just appears blank on the preview screen.
Is there any way to show content inside a FragmentContainerView
when previewing it in a layout in Android Studio?
In my case the preview was not working even after adding tools:layout="@layout/...
. I also had to add android:name="packageName + name of the fragment I wanted to show in preview"
.
So the final XML looked something like this:
<androidx.fragment.app.FragmentContainerView
android:id="@+id/someFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.packageName.FragmentName"
tools:layout="@layout/fragment_name" />
As stated in the comment by tir38 this was fixed in later versions of Android Studio, you can now use the tools:layout attribute in the FragmentContainerView. In cases where you are using Navigation component, if you define a startDestination for your Navigation graph which uses the tools:layout attribute, this will automatically be reflected in your FragmentContainerView
<androidx.fragment.app.FragmentContainerView
...
app:navGraph="@navigation/nav_graph"/>
<navigation
...
app:startDestination="@id/someFragment">
<fragment
...
android:id="@+id/someFragment"
tools:layout="@layout/fragment_splash"/>
</navigation>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With