Home > OS >  Design view breaks as soon as I add any FragmentContainerView
Design view breaks as soon as I add any FragmentContainerView

Time:01-19

Here you can see the split screen design view working when I comment out the FragmentContainerView:

enter image description here

And as soon as I uncomment it, the design view breaks

enter image description here

There are no errors as far as I can tell.

The full xml, just showing the structure and not the sibling content:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context=".activities.ParametersActivity" android:layout_height="match_parent"
    android:layout_width="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_marginStart="10dp"
            android:layout_marginEnd="10dp"
            android:layout_height="wrap_content"
            android:orientation="vertical">
             <LinearLayout
                 android:id="@ id/colorLayout"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:visibility="gone"
                 android:orientation="vertical">
        
                  <androidx.fragment.app.FragmentContainerView
                      xmlns:android="http://schemas.android.com/apk/res/android"
                      android:layout_height="match_parent"
                      android:layout_width="match_parent"
                      />
             </LinearLayout>
         </LinearLayout>
     </ScrollView>
</LinearLayout>

What's interesting is that the FragmentContainerView does seem to actually be working when I build the app. So I bet this is just an Android Studio bug.

What can I do to debug and resolve this?

CodePudding user response:

The layout error prompted by Android Studio is:- Layout Error

This can be resolved in the following ways:-

  • If you are using the JetPack Navigation library and have a navGraph layout file, then specify it under the app:navGraph tag as :
        <androidx.fragment.app.FragmentContainerView
            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"
            app:navGraph="@navigation/nav_graph" />
  • If a single fragment is required to be displayed, then the same can be done by specifying its name under the android:name tag as :
        <androidx.fragment.app.FragmentContainerView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:name="com.example.YourFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
  • In the latter approach, however, as the error image says, a specific layout can not be shown by FragmentContainerView in edit mode. You can have a preview of the Fragment displayed by tools:layout tag.
        <androidx.fragment.app.FragmentContainerView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:name="com.example.YourFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:layout="@layout/fragment_your_fragment"/>

This should solve the problem, the layout is clearly visible on my IDE now!

  • Related