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"
How to create a layout with rounded corners on only the top or bottom half?
You can achieve this using material cardview. Here's the xml code of the full layout file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.card.MaterialCardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="10dp"
app:cardCornerRadius="30dp"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#406cca"
android:visibility="visible" />
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#fb7500" />
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
</androidx.constraintlayout.widget.ConstraintLayout>
Remember, if the image is not there, just make the visibility of that particular imageview in your recyclerview to View.GONE in your code.
I hope you understood. If any queries, put a comment!
Here a pic of what this code looks like:
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?
How to make a view with rounded corners?
Another approach is to make a custom layout class like the one below. This layout first draws its contents to an offscreen bitmap, masks the offscreen bitmap with a rounded rect and then draws the offscreen bitmap on the actual canvas.
I tried it and it seems to work (at least for my simple testcase). It will of course affect performance compared to a regular layout.
package com.example;
import android.content.Context;
import android.graphics.*;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.widget.FrameLayout;
public class RoundedCornerLayout extends FrameLayout {
private final static float CORNER_RADIUS = 40.0f;
private Bitmap maskBitmap;
private Paint paint, maskPaint;
private float cornerRadius;
public RoundedCornerLayout(Context context) {
super(context);
init(context, null, 0);
}
public RoundedCornerLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, 0);
}
public RoundedCornerLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs, defStyle);
}
private void init(Context context, AttributeSet attrs, int defStyle) {
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
cornerRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, CORNER_RADIUS, metrics);
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
maskPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
setWillNotDraw(false);
}
@Override
public void draw(Canvas canvas) {
Bitmap offscreenBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
Canvas offscreenCanvas = new Canvas(offscreenBitmap);
super.draw(offscreenCanvas);
if (maskBitmap == null) {
maskBitmap = createMask(canvas.getWidth(), canvas.getHeight());
}
offscreenCanvas.drawBitmap(maskBitmap, 0f, 0f, maskPaint);
canvas.drawBitmap(offscreenBitmap, 0f, 0f, paint);
}
private Bitmap createMask(int width, int height) {
Bitmap mask = Bitmap.createBitmap(width, height, Bitmap.Config.ALPHA_8);
Canvas canvas = new Canvas(mask);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.WHITE);
canvas.drawRect(0, 0, width, height, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
canvas.drawRoundRect(new RectF(0, 0, width, height), cornerRadius, cornerRadius, paint);
return mask;
}
}
Use this like a normal layout:
<com.example.RoundedCornerLayout
android:layout_width="200dp"
android:layout_height="200dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/test"/>
<View
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#ff0000"
/>
</com.example.RoundedCornerLayout>
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 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.
Related Topics
Android Imageview Scaling and Translating Issue
How to Pick an Image from Gallery (Sd Card) For My App
This Activity Already Has an Action Bar Supplied by the Window Decor
How to Implement a Viewpager With Different Fragments/Layouts
Focusable Edittext Inside Listview
Example: Communication Between Activity and Service Using Messaging
How to Define Dimens.Xml For Every Different Screen Size in Android
How to Connect to My Http://Localhost Web Server from Android Emulator
How to Set the Part of the Text View Is Clickable
Get/Pick an Image from Android'S Built-In Gallery App Programmatically
Difference Between Activity Context and Application Context
How to Close Android Application
How to Tell When a Layout Has Been Drawn
Detect Home Button Press in Android
Java.Util.Zip.Zipexception: Duplicate Entry During Packagealldebugclassesformultidex