Search code examples
androidandroid-layoutandroid-framelayout

How to stop frame layout background from showing through fragment controls?


I have a frame layout with a background color set that I programmaticaly add fragments to. The fragments are smaller than the layout and I want the layout color to show around the edges of the fragments but when I add the fragments the layout's background color eliminates the background color of the frgament's parts. Is there a way where I can have the fragment's parts come to the fore and the layout color stay in the background?

Thanks


Solution

  • Try adding the drawable background to the frame layout you are trying to inflate to the main layout::

    <FrameLayout
      android:layout_height=""
      android:layout_width=""
      android:background="@drawable/frame_drawable">
       .....
       //Frame Layout contents
       .....
    </FrameLayout>
    

    Create a drawable file named frame_drawable.xml on your drawable folder::

        <?xml version="1.0" encoding="utf-8"?>
        <shape xmlns:android="http://schemas.android.com/apk/res/android" 
                     android:shape="rectangle">
    
            <!--Background-->
            <solid android:color="@android:color/transparent"></solid>
            <!--Border-->
            <stroke android:width="1dp" android:color="@color/mainBackground"></stroke>
            <!--Radius-->
            <corners android:radius="3dp"></corners>
    
        </shape>