Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equivalent of "tools:layout=" for FragmentContainerView?

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?

like image 454
Julio E. Rodríguez Cabañas Avatar asked Aug 31 '25 15:08

Julio E. Rodríguez Cabañas


2 Answers

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" />
like image 141
ganjaam Avatar answered Sep 12 '25 12:09

ganjaam


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>
like image 42
Mats Avatar answered Sep 12 '25 11:09

Mats



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!