Gabor Meszaros
Gabor Meszaros

Reputation: 1325

How to change a background shape's color programmatically

I have a custom view where I have to change the background shape's color from the code based on the input.

The shape:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <size
        android:width="64dp"
        android:height="64dp" />
    <solid
        android:color="#BBBBBB" />
</shape>

The custom view:

public class MyCustomView extends TextView {

    public MyCustomView(Context context) {
        super(context);
    }

    public MyCustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyCustomView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MyCustomView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public void setCustomValue(long customValue) {
        this.setBackgroundResource(R.drawable.shape_background);
        this.setTextColor(getColor(R.color.colorCustomViewText));
        this.setGravity(Gravity.CENTER);
        this.setText(String.valueOf(customValue));
    }

    public long getCustomValue() {
        return Long.parseLong(this.getText().toString());
    }

    private int getColor(int colorId) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return getContext().getColor(colorId);
        } else {
            return getContext().getResources().getColor(colorId);
        }
    }
}

The color of the shape always #BBBBBB. I cannot change it to e.g. red no matter which color related attribute I try. I have to change the color based on the input.

Upvotes: 0

Views: 1013

Answers (3)

Bhavnik
Bhavnik

Reputation: 2048

Try below code, put this code in your customtextview class and change background color as per your requirement.

GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[]{ContextCompat.getColor(this,R.color.red_500),
            ContextCompat.getColor(this,R.color.red_500), ContextCompat.getColor(this,R.color.red_500)});
    gd.setShape(GradientDrawable.RECTANGLE);
    gd.setStroke((int)0.5, ContextCompat.getColor(this, R.color.black));
    gd.setCornerRadius(8f);
    gd.setBounds(2, 2, 2, 2);
    btnCreateUser.setBackground(gd);

Here is a screenshot

Upvotes: 2

Magesh Pandian
Magesh Pandian

Reputation: 9369

You can change color using GradientDrawable Api,

Xml,

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/outerRectangle">
    <shape android:shape="oval" >
        <solid android:color="#FCD366" />

        <stroke
            android:width="1dp"
            android:color="@android:color/darker_gray" />
    </shape>
</item>

In Java,

  Drawable tempDrawable = getResources().getDrawable(R.drawable.library_cirecle);
LayerDrawable bubble = (LayerDrawable) tempDrawable; (cast to root element in xml)
    GradientDrawable solidColor = (GradientDrawable) bubble.findDrawableByLayerId(R.id.outerRectangle);
    solidColor.setColor(newColorCode);
    imageView.setImageDrawable(tempDrawable);

For more info see below link,

Link1 Link2

Upvotes: 2

Nicolas
Nicolas

Reputation: 7081

You can set a color filter to your shape drawable:

Drawable background = getContext().getResources().getDrawable(R.drawable.shape_background);
background.setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
this.setBackground(background);

Upvotes: 2

Related Questions