Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically set custom attributes of custom components?

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")

like image 702
mz496 Avatar asked Aug 27 '15 23:08

mz496


People also ask

How do I create a custom attribute?

AttributeUsage has a named parameter, AllowMultiple , with which you can make a custom attribute single-use or multiuse. In the following code example, a multiuse attribute is created. In the following code example, multiple attributes of the same type are applied to a class. [Author("P.

How would you define a custom attribute that can only be applied to class code elements?

The following code fragment specifies that a custom attribute can be applied to any class or method: C# Copy. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] End Class.

What is Attrs XML in Android?

An <attr> element has two xml attributes name and format . name lets you call it something and this is how you end up referring to it in code, e.g., R. attr. my_attribute . The format attribute can have different values depending on the 'type' of attribute you want.

What is the use of custom attributes in C#?

Attributes are used to impose conditions or to increase the efficiency of a piece of code. There are built-in attributes present in C# but programmers may create their own attributes, such attributes are called Custom attributes. To create custom attributes we must construct classes that derive from the System.


1 Answers

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.)

like image 86
mz496 Avatar answered Oct 19 '22 20:10

mz496