Home » Android » android – FrameLayout with rounded corners

android – FrameLayout with rounded corners

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a FrameLayout inside my root view (linear:vertical) that I’m using to hold different fragments depending on user input. The FrameLayout is smaller than the background, so it appears to be “floating”.

How do I round the corners of the FrameLayout?

The frags that go inside the FrameLayout are not pictures, so I can’t crop them.

How to&Answers:

Create an xml file, e.g. your_rounded_shape.xml, in your drawable folder and add the following code:

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid android:color="#CCCCCC"/>    

    <stroke android:width="2dp"
        android:color="#999999"/>

    <padding android:left="2dp"
         android:top="2dp"
         android:right="2dp"
         android:bottom="2dp"/> 

    <corners android:bottomRightRadius="8dp" 
         android:bottomLeftRadius="8dp" 
         android:topLeftRadius="8dp" 
         android:topRightRadius="8dp"/> 
</shape>

Then use your_rounded_shape.xml as a background in your FrameLayout. Something like this:

<FrameLayout
    android:width="match_parent"
    android:height="wrap_content"
    android:background="your_rounded_shape"/>

Answer:

Replace the FrameLayout with CardView layout.

<android.support.v7.widget.CardView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:cardCornerRadius="8dp">

</android.support.v7.widget.CardView>

Answer:

Just an improvement of the above answer:
name of this xml file in let’s say rounded_frame_layout.xml

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid android:color="#CCCCCC"/>    

    <stroke android:width="2dp"
        android:color="#999999"/>

    <padding android:left="2dp"
         android:top="2dp"
         android:right="2dp"
         android:bottom="2dp"/> 

    <corners android:radius="8dp"/> 
</shape>


Then use your drawable as follows:

<FrameLayout
     android:width="match_parent"
     android:height="wrap_content"
     android:background="@drawable/rounded_frame_layout"/>