How to Apply Corner Radius to LinearLayout
You can create an XML file in the drawable folder. Call it, for example, shape.xml
In shape.xml
:
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid
android:color="#888888" >
</solid>
<stroke
android:width="2dp"
android:color="#C4CDE0" >
</stroke>
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp" >
</padding>
<corners
android:radius="11dp" >
</corners>
</shape>
The <corner>
tag is for your specific question.
Make changes as required.
And in your whatever_layout_name.xml
:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="5dp"
android:background="@drawable/shape" >
</LinearLayout>
This is what I usually do in my apps.
How to make layout with rounded corners..?
1: Define layout_bg.xml in drawables:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
<stroke android:width="3dp" android:color="#B1BCBE" />
<corners android:radius="10dp"/>
<padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
2: Add layout_bg.xml
as background to your layout
android:background="@drawable/layout_bg"
Rounded corners on linearlayout
I want to make the whole view (linearlayout) with rounded corners.
Since the LinearLayout
covers all the CardView
, then the whole view, is the CardView.
So instead of setting transparent background to the CardView
, set it to the LinearLayout
, set a background color to the CardView
and you will see rounded corners.
If you set transparent color to both the CardView
and the LinearLayout
how do you expect to see rounded corners since there are no corners?
LinearLayout with rounded corners and background color
From the code samples you provide us, it doesn't seem necessary to me to have two LinearLayout. Only the layout with android:background="@drawable/custom_border"
is required.
To achieve the expected result, just add the property <solid android:color="@color/whitegrey" />
to your custom_border.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="20dp"/>
<padding android:left="8dp" android:right="8dp" android:top="8dp" android:bottom="8dp"/>
<stroke android:width="2dp" android:color="#444444" />
<solid android:color="@color/whitegrey" />
</shape>
I wish I was able to help you!
How to create corner radius in top left and bottom right in android?
With the Material Components Library you can define custom CornerTreatment
.
For example you can use a CardView
and apply to it a ShapeAppearanceModel
.
<LinearLayout
android:background="@color/colorPrimaryLight"
android:clipChildren="false"
android:clipToPadding="false"
..>
<com.google.android.material.card.MaterialCardView
android:id="@+id/card"
app:cardCornerRadius="32dp"
app:cardBackgroundColor="@color/colorPrimaryDark"
../>
</LinearLayout>
Then:
MaterialCardView cardView = findViewById(R.id.card);
cardView.setShapeAppearanceModel(cardView.getShapeAppearanceModel().toBuilder()
.setTopLeftCorner(new CrazyCornerTreatment())
.setBottomLeftCorner(CornerFamily.ROUNDED,0f)
.setBottomRightCorner(CornerFamily.ROUNDED,0f)
.build());
The top left corner can be defined with:
class CrazyCornerTreatment : CornerTreatment() {
override fun getCornerPath(
shapePath: ShapePath,
angle: Float,
interpolation: Float,
radius: Float
) {
val interpolatedRadius = radius * interpolation
shapePath.reset(0f, -radius * interpolation, 270f,270 -angle)
shapePath.addArc(
0f,
-2*interpolatedRadius,
2*interpolatedRadius,
0f,
180f,
- angle)
}
}
Adding a color background and border radius to a Layout
You don't need the separate fill item. In fact, it's invalid. You just have to add a solid
block to the shape
. The subsequent stroke
draws on top of the solid
:
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="5dp" />
<solid android:color="@android:color/white" />
<stroke
android:width="1dip"
android:color="@color/bggrey" />
</shape>
You also don't need the layer-list
if you only have one shape
.
Rounded linear layout leaving extra space on corners
With below code you have rounded background with least space on corner
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle">
<solid android:color="#FFFFFF" />
<corners android:radius="5dp" />
<stroke
android:width="2dip"
android:color="#275D69" />
</shape>
this may include space on corner but it's too low.
just reduce radius
value.
Related Topics
How to Implement Google Maps Search by Address in Android
Download and Extract Zip File in Android
Android Studio Image Asset Launcher Icon Background Color
Why a Viewmodel Factory Is Needed in Android
Adding Custom Font to Theme in Android
Android Audiorecord Supported Sampling Rates
Android 4.0, Text on the Action Bar Never Shows
How to Stop Scrolling in a Gallery Widget
Making a Linearlayout Act Like an Button
Otp (Token) Should Be Automatically Read from the Message
Don't Collapse Toolbar When Recyclerview Fits the Screen
Running Emulator After Building Android from Source
How to Change App Name Per Gradle Build Type
Okhttp Response Callbacks on the Main Thread
Writing/Reading Files To/From Android Phone's Internal Memory