Unknown attribute class warning with FragmentContainerView

2.5k views Asked by At

I have a SettingsFragment class that hosts a PreferenceFragment for the app settings.

In layout, I used FragmentContainerView which points to that PreferenceFragment class with the class attribute.

<androidx.fragment.app.FragmentContainerView
    class="xxxxxx.fragments.SettingsFragment$PreferenceFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

Backing to normal fragment instead of FragmentContainerView the warning is gone, so it turns out that it's related to FragmentContainerView.

How can I get rid of this warning while using FragmentContainerView?

3

There are 3 answers

1
Kishan Maurya On BEST ANSWER

It should be android:name="xxxxxx.fragments.SettingsFragment$PreferenceFragment" as per FragmentContainerView documentation.

<androidx.fragment.app.FragmentContainerView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/fragment_container_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="xxxxxx.fragments.SettingsFragment$PreferenceFragment"
        android:tag="my_tag">
 </androidx.fragment.app.FragmentContainerView>
 
0
Firzen On

Unknown fragments error solution for Android Studio 2022.1.1 Patch 1 (Electric Eel):

Click the red icon with exclamation mark.

Step 1

In bottom panel with errors found in layout, select Unknown fragments.

Step 2

In right part of bottom panel, click the link Do not warn about <android.fragment.app. FragmentContainerView> tags in this session

Step 3

Congratulations! Your layout preview is now working again!

NOTE: Sadly, this solution only works until you close Android Studio. If anyone knows about permanent solution, please let me know in comments and I will update this answer.

1
Taiwo Oluwagbemiga On

For Unknown fragments error solution for Android Studio 2022.1.1 Patch 1 (Electric Eel):

use this:

<androidx.fragment.app.FragmentContainerView
    android:id="@+id/fragment_container_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="xxxxxx.xxxxxx.FragmentName"
    tools:layout="@layout/fragment_name"
/>

Remember, add the tools:layout...