I would like edit the background of layout in runtime. My image drawable is in the class and I would like to set the image with corners like background of the layout. How to do this?
-
post the code please. – Roman Rozenshtein Jan 29 '16 at 13:24
-
I like to knows the code I must write. – Augusto Feb 01 '16 at 11:00
-
check http://stackoverflow.com/questions/9517434/changing-image-as-rounded-corner – Jaiprakash Soni Feb 03 '16 at 07:26
5 Answers
- First get the background from your view using getBackground().
- Cast it to a GradientDrawable variable.
- Finally call method setCornerRadius(float value) with the value.
setCornerRadius(float value) will set all four corners with the same value.
So it also has the method setCornerRadii(float []radii), which can be used to set corners of all four sides, from top-left, top-right, bottom-right, bottom-left;
setCornerRadii(float []radii)
Specify radii for each of the 4 corners. For each corner, the array contains 2 values, [X_radius, Y_radius]. The corners are ordered top-left, top-right, bottom-right, bottom-left. This property is honored only when the shape is of type RECTANGLE.
GradientDrawable drawable = (GradientDrawable) view.getBackground();
drawable.setCornerRadii(radii);
OR
float values[] = {1.1f, 2.2f, 1.5f, 3.3f};
GradientDrawable drawable = (GradientDrawable) view.getBackground();
drawable.setCornerRadii(values);
This means, top-left corner is 1.1f, top-right corner is 2.2, bottom-right corner is 1.5f and finally bottom-left corner is 3.3f.

- 2,618
- 15
- 13

- 10,335
- 2
- 41
- 41
Try this:
GradientDrawable bg = (GradientDrawable) relative_layout.getBackground();
bg.setCornerRadii();

- 2,618
- 15
- 13

- 684
- 5
- 13
-
How I get the relativeLayout background if I want set to relativeLayout background? – Augusto Feb 01 '16 at 10:36
-
do u want to set background or you want to set rounded corners to the background? – inkedTechie Feb 01 '16 at 10:38
-
-
add this line before the two lines `relativeLayout.setBackground("bcakground Image");` – inkedTechie Feb 01 '16 at 10:41
-
I don't understand, the GradientDrawable have no connection with the background of relativeLayout, is a other variable, other source, other all! – Augusto Feb 01 '16 at 10:48
-
-
besides:java.lang.ClassCastException: android.graphics.drawable.BitmapDrawable cannot be cast to android.graphics.drawable.GradientDrawable – Augusto Feb 01 '16 at 10:50
-
I try: RelativeLayout.setBackground(receita.foto); GradientDrawable drawable = (GradientDrawable) RelativeLayout.getBackground(); drawable.setCornerRadius(5); RelativeLayout.setBackground(drawable); – Augusto Feb 01 '16 at 10:51
-
for bitmap, you can go to this link http://stackoverflow.com/questions/11012556/border-over-a-bitmap-with-rounded-corners-in-android – inkedTechie Feb 01 '16 at 11:09
Create drawables resource for your normal and curved background and place them in res/drawable/
folder.
For example:
box.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
</shape>
box_curved.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="5dp" />
</shape>
Then at runtime, change background by :
view.setBackground(getResources().getDrawable(R.drawable.box));
or
view.setBackground(getResources().getDrawable(R.drawable.box_curved));

- 739
- 5
- 11
-
he wants to set corners of drawable at runtime not to change drawable at runtime my friend. – Vishal Patoliya ツ Jan 29 '16 at 13:33
-
I believe that too will follow the same logic, just that he needs to use an ImageView with background set dynamically like I said, and src set to the image drawable. – TeChNo_DeViL Jan 29 '16 at 13:38
int sdk = android.os.Build.VERSION.SDK_INT; if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) { setBackgroundDrawable(); } else { setBackground(); }
To create Shape Drawable Programetically
public static void createShapeDrawable(View v, int backgroundColor, int borderColor)
{
GradientDrawable shape = new GradientDrawable();
shape.setShape(GradientDrawable.RECTANGLE);
shape.setCornerRadii(new float[] { 8, 8, 8, 8, 0, 0, 0, 0 }); // set corner Radious
shape.setColor(backgroundColor);
shape.setStroke(3, borderColor);
v.setBackgroundDrawable(shape);
}

- 1,889
- 1
- 19
- 27