20

I have a custom component called CircleView, and I want to change a custom attribute called fillColor defined in attrs.xml:

<declare-styleable name="CircleView">
    <attr name="radius" format="integer" />
    <attr name="fillColor" format="color" />
</declare-styleable>

I have set it initially in my layout XML, which currently looks like this (namespace circleview is defined as xmlns:circleview="http://schemas.android.com/apk/res-auto"; it works fine when I define it in the XML, so this shouldn't be an issue):

<com.mz496.toolkit.CircleView
     ...
     circleview:fillColor="#33ffffff"/>

I can get the fillColor attribute just fine in my CircleView, which extends View, but I don't know how to set its value.

I've investigated things like setBackgroundColor and looked for other "set" methods, but I couldn't find any. I imagined a method like

circle.setAttribute(R.styleable.CircleView_fillColor, "#33ff0000")

mz496
  • 790
  • 1
  • 7
  • 13
  • You might also find answers here : https://stackoverflow.com/questions/56560657/setting-the-value-for-custom-attribute-android – Jack' May 26 '20 at 18:09

1 Answers1

9

A CircleView in the layout is nothing more than an instance of the CircleView class, so simply add a function into CircleView.java:

public void setFillColor(int newColor) {
    fillColor = newColor;
}

And then call it whenever needed:

CircleView circle_view = (CircleView) findViewById(R.id.circle_view);
circle_view.setFillColor(0x33ffffff);
circle_view.invalidate();

Also note that this just changes an internal variable, but you still need to redraw the custom component using the invalidate() method of the View class, since the custom component is only redrawn automatically if the entire view is redrawn, e.g. when switching fragments (see: Force a View to redraw itself).

(I figured this out at the very end when I was just about to ask, "Would I need to define this myself?" And I tried defining it myself, and it worked.)

Community
  • 1
  • 1
mz496
  • 790
  • 1
  • 7
  • 13
  • Yeah, there's nothing in the stock Android tooling that automagically adds accessors corresponding to attributes, in part because that might not be the right answer for all use cases. I'm not aware of any third-party libraries that offer this, but I could certainly seem somebody rolling an annotation processor that tried to do this sort of code generation. – CommonsWare Aug 28 '15 at 00:04
  • Right, Android Studio silently tries to help by autocompleting method names (after typing `public void set`, it suggests `set`, but I think that's all that happens for now. – mz496 Aug 28 '15 at 01:52
  • @mz496 the above doesnt work for attribites defined in the xml – Subham Burnwal Feb 20 '19 at 01:13