1

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?

Augusto
  • 3,825
  • 9
  • 45
  • 93

5 Answers5

4
  1. First get the background from your view using getBackground().
  2. Cast it to a GradientDrawable variable.
  3. 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.

hair raisin
  • 2,618
  • 15
  • 13
capt.swag
  • 10,335
  • 2
  • 41
  • 41
1

Try this:

GradientDrawable bg = (GradientDrawable) relative_layout.getBackground();
bg.setCornerRadii();
hair raisin
  • 2,618
  • 15
  • 13
inkedTechie
  • 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
  • The 2. A image of background with rounded corners :) – Augusto Feb 01 '16 at 10:39
  • 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
  • type casting is necessary to use setCornerRadii method – inkedTechie Feb 01 '16 at 10:49
  • 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
0

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));
TeChNo_DeViL
  • 739
  • 5
  • 11
0
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
    setBackgroundDrawable();
} else {
    setBackground();
}

see : https://stackoverflow.com/a/11947755/3329488

Community
  • 1
  • 1
Mo1989
  • 2,374
  • 1
  • 15
  • 17
0

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);
    }
Divyang Panchal
  • 1,889
  • 1
  • 19
  • 27