FragmentContainerView

public final class FragmentContainerView
extends FrameLayout

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.FrameLayout
         ↳ androidx.fragment.app.FragmentContainerView


FragmentContainerView is a customized Layout designed specifically for Fragments. It extends FrameLayout, so it can reliably handle Fragment Transactions, and it also has additional features to coordinate with fragment behavior.

FragmentContainerView should be used as the container for Fragments, commonly set in the xml layout of an activity, e.g.:

 <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">
 </androidx.fragment.app.FragmentContainerView>
 

FragmentContainerView can also be used to add a Fragment by using the android:name attribute. FragmentContainerView will perform a one time operation that:

You can optionally include an android:tag which allows you to use FragmentManager.findFragmentByTag(String) to retrieve the added Fragment.

 <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="com.example.MyFragment"
        android:tag="my_tag">
 </androidx.fragment.app.FragmentContainerView>
 

FragmentContainerView should not be used as a replacement for other ViewGroups (FrameLayout, LinearLayout, etc) outside of Fragment use cases.

FragmentContainerView will only allow views to returned by a Fragment's Fragment.onCreateView(LayoutInflater, ViewGroup, Bundle). Attempting to add any other view will result in an IllegalStateException.

Layout animations and transitions are disabled for FragmentContainerView for APIs above 17. Otherwise, Animations should be done through FragmentTransaction.setCustomAnimations(int, int, int, int). IfanimateLayoutChanges is set to true or setLayoutTransition(LayoutTransition) is called directly an UnsupportedOperationException will be thrown.

Fragments using exit animations are drawn before all others for FragmentContainerView. This ensures that exiting Fragments do not appear on top of the view.

Summary

Inherited constants

Inherited fields

Public constructors

FragmentContainerView(Context context)
FragmentContainerView(Context context, AttributeSet attrs)

Do not call this constructor directly.

FragmentContainerView(Context context, AttributeSet attrs, int defStyleAttr)

Do not call this constructor directly.

Public methods

void addView(View child, int index, ViewGroup.LayoutParams params)

FragmentContainerView will only allow views to returned by a Fragment's Fragment.onCreateView(LayoutInflater, ViewGroup, Bundle).

void endViewTransition(View view)
WindowInsets onApplyWindowInsets(WindowInsets insets)

The sys ui flags must be set to enable extending the layout into the window insets.

void removeAllViewsInLayout()
void removeView(View view)
void removeViewAt(int index)
void removeViewInLayout(View view)
void removeViews(int start, int count)
void removeViewsInLayout(int start, int count)
void setLayoutTransition(LayoutTransition transition)

When called, this method throws a UnsupportedOperationException on APIs above 17.

void startViewTransition(View view)

Protected methods

boolean addViewInLayout(View child, int index, ViewGroup.LayoutParams params, boolean preventRequestLayout)

FragmentContainerView will only allow views to returned by a Fragment's Fragment.onCreateView(LayoutInflater, ViewGroup, Bundle).

void dispatchDraw(Canvas canvas)
boolean drawChild(Canvas canvas, View child, long drawingTime)
void removeDetachedView(View child, boolean animate)

Inherited methods

Public constructors

FragmentContainerView

public FragmentContainerView (Context context)

Parameters
context Context

FragmentContainerView

public FragmentContainerView (Context context, 
                AttributeSet attrs)

Do not call this constructor directly. Doing so will result in an UnsupportedOperationException.

Parameters
context Context

attrs AttributeSet

FragmentContainerView

public FragmentContainerView (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Do not call this constructor directly. Doing so will result in an UnsupportedOperationException.

Parameters
context Context

attrs AttributeSet

defStyleAttr int

Public methods

addView

public void addView (View child, 
                int index, 
                ViewGroup.LayoutParams params)

FragmentContainerView will only allow views to returned by a Fragment's Fragment.onCreateView(LayoutInflater, ViewGroup, Bundle). Attempting to add any other view will result in an IllegalStateException.

Parameters
child View

index int

params ViewGroup.LayoutParams

endViewTransition

public void endViewTransition (View view)

Parameters
view View

onApplyWindowInsets

public WindowInsets onApplyWindowInsets (WindowInsets insets)

The sys ui flags must be set to enable extending the layout into the window insets.

Parameters
insets WindowInsets

Returns
WindowInsets

removeAllViewsInLayout

public void removeAllViewsInLayout ()

removeView

public void removeView (View view)

Parameters
view View

removeViewAt

public void removeViewAt (int index)

Parameters
index int

removeViewInLayout

public void removeViewInLayout (View view)

Parameters
view View

removeViews

public void removeViews (int start, 
                int count)

Parameters
start int

count int

removeViewsInLayout

public void removeViewsInLayout (int start, 
                int count)

Parameters
start int

count int

setLayoutTransition

public void setLayoutTransition (LayoutTransition transition)

When called, this method throws a UnsupportedOperationException on APIs above 17. On APIs 17 and below, it calls ViewGroup.setLayoutTransition(LayoutTransition) This can be called either explicitly, or implicitly by setting animateLayoutChanges to true.

View animations and transitions are disabled for FragmentContainerView for APIs above 17. Use FragmentTransaction.setCustomAnimations(int, int, int, int) and FragmentTransaction.setTransition(int).

Parameters
transition LayoutTransition: The LayoutTransition object that will animated changes in layout. A value of null means no transition will run on layout changes.

startViewTransition

public void startViewTransition (View view)

Parameters
view View

Protected methods

addViewInLayout

protected boolean addViewInLayout (View child, 
                int index, 
                ViewGroup.LayoutParams params, 
                boolean preventRequestLayout)

FragmentContainerView will only allow views to returned by a Fragment's Fragment.onCreateView(LayoutInflater, ViewGroup, Bundle). Attempting to add any other view will result in an IllegalStateException.

Parameters
child View

index int

params ViewGroup.LayoutParams

preventRequestLayout boolean

Returns
boolean

dispatchDraw

protected void dispatchDraw (Canvas canvas)

Parameters
canvas Canvas

drawChild

protected boolean drawChild (Canvas canvas, 
                View child, 
                long drawingTime)

Parameters
canvas Canvas

child View

drawingTime long

Returns
boolean

removeDetachedView

protected void removeDetachedView (View child, 
                boolean animate)

Parameters
child View

animate boolean