How do I set the rounded corner radius of a color drawable using xml?
Use the <shape>
tag to create a drawable in XML with rounded corners. (You can do other stuff with the shape tag like define a color gradient as well).
Here's a copy of a XML file I'm using in one of my apps to create a drawable with a white background, black border and rounded corners:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffffff"/>
<stroke android:width="3dp"
android:color="#ff000000" />
<padding android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp" />
<corners android:radius="7dp" />
</shape>
Programatically change background of Drawable, maintaining the corner radius
Since you are just using the shape to create a layout with rounded corners and a border, the first option is to wrap your LinearLayout
inside a CardView
and then apply to the card the corner radius, the stroke and the background color.
Otherwise you could use the MaterialShapeDrawable
included in the Material Components Library to draw custom shapes.
Just remove from the LinearLayout the android:background
:
<LinearLayout
android:id="@+id/interviewTimelineIconLayout"
android:layout_width=".."
android:layout_height="..
..>
<!-- ..... -->
</LinearLayout>
Then in your code you can apply a ShapeAppearanceModel
. Something like:
float radius = getResources().getDimension(R.dimen.corner_radius);
LinearLayout linearLayout= findViewById(R.id.interviewTimelineIconLayout);
ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel()
.toBuilder()
.setAllCorners(CornerFamily.ROUNDED,radius)
.build();
MaterialShapeDrawable shapeDrawable = new MaterialShapeDrawable(shapeAppearanceModel);
//Fill the LinearLayout with your color
shapeDrawable.setFillColor(ContextCompat.getColorStateList(this,R.color.yourColor));
//Stroke color and width
shapeDrawable.setStrokeWidth(2.0f);
shapeDrawable.setStrokeColor(...);
ViewCompat.setBackground(linearLayout,shapeDrawable);
In this way you easily change and set the color background and the stroke.
How to programmatically round corners and set random background colors
Instead of setBackgroundColor
, retrieve the background drawable and set its color:
v.setBackgroundResource(R.drawable.tags_rounded_corners);
GradientDrawable drawable = (GradientDrawable) v.getBackground();
if (i % 2 == 0) {
drawable.setColor(Color.RED);
} else {
drawable.setColor(Color.BLUE);
}
Also, you can define the padding within your tags_rounded_corners.xml
:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="4dp" />
<padding
android:top="2dp"
android:left="2dp"
android:bottom="2dp"
android:right="2dp" />
</shape>
Rounded corners in layout with custom color on Android
To programmatically change a color globally defined in a shape, use this:
findViewById(R.id.your_xml_element).getBackground().setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_ATOP);
Android: drawable with perfect round corner
Try this. Works perfectly for me.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#A5000000"/>
<stroke android:width="0dp"
android:color="#00FFFFFF"/>
<padding android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"/>
<corners android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"/>
</shape>
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"
Android - drawable with rounded corners at the top only
Try giving these values:
<corners android:topLeftRadius="6dp" android:topRightRadius="6dp"
android:bottomLeftRadius="0.1dp" android:bottomRightRadius="0.1dp"/>
Note that I have changed 0dp
to 0.1dp
.
EDIT: See Aleks G comment below for a cleaner version
How to make an ImageView with rounded corners?
This is pretty late in response, but for anyone else that is looking for this, you can do the following code to manually round the corners of your images.
http://www.ruibm.com/?p=184
This isn't my code, but I've used it and it's works wonderfully. I used it as a helper within an ImageHelper class and extended it just a bit to pass in the amount of feathering I need for a given image.
Final code looks like this:
package com.company.app.utils;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
public class ImageHelper {
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = pixels;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
}
Related Topics
Rotating Image. Animation List or Animated Rotate? (Android)
Db File in Assets Folder. Will It Be Updated
How to Set Proxysettings and Proxyproperties on Android Wi-Fi Connection Using Java
Change Button Text and Action - Android Development
Linearlayout Height in Oncreate Is 0
How to Place App Icon on Launcher Home Screen
How to Add Radio Button Dynamically as Per The Given Number of Counts
How to Get My Android Device Internal Download Folder Path
Shouldoverrideurlloading in Webview for Android Not Running
How to Restart Service After The App Is Killed from Recent Tasks
No Resource Found That Matches The Given Name '@Style/ Theme.Holo.Light.Darkactionbar'
How to Pick Image for Crop from Camera or Gallery in Android 7.0
How to Run a Specific Android App Using Terminal
Pdf Library for Android - Pdfbox
How to Display a One Time Welcome Screen
Access The Sim Card with an Android Application
Android Convert Px to Dp (Video Aspect Ratio)
How to Put My Libraries in Front of Android.Jar by Editing Build.Gradle in Android-Studio