-1

I have a shape in circle.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    >
    <solid android:color="@color/colorAccent" />
    <size
        android:width="200dp"
        android:height="200dp" />
    <corners android:radius="100dp" />
</shape>

How do i refer it from java and change its color ?

Xera12
  • 201
  • 1
  • 2
  • 9
  • Take a look at this: https://stackoverflow.com/a/14789329/7591918 - As the linked answer states, you can use JAXB. Just make a class containing all the necessary data, convert your XML file to a POJO and change it's color as a value from within your app. – Wep0n Jun 28 '17 at 05:36

1 Answers1

1

Try creating shape in java without xml.

GradientDrawable gradientDrawable=new GradientDrawable();
gradientDrawable.setShape(GradientDrawable.OVAL);
gradientDrawable.setColor(getResources().getColor(R.color.colorAccent));
gradientDrawable.setSize(200,200);
gradientDrawable.setCornerRadius(100);

Or you can inflate the existing shape from xml and change it's properties like this,

GradientDrawable shapeDrawable= (GradientDrawable) ContextCompat.getDrawable(this,R.drawable.shape);
shapeDrawable.setColor(getResources().getColor(R.color.colorPrimary));
imageView.setImageDrawable(shapeDrawable);
Jinesh Francis
  • 3,377
  • 3
  • 22
  • 37