When creating a custom component in android it is often asked how to create and pass through the attrs property to the constructor.
It is often suggested that when creating a component in java that you simply use the default constructor, i.e.
new MyComponent(context);
rather than attempting to create an attrs object to pass through to the overloaded constructor often seen in xml based custom components. I've tried to create an attrs object and it doesn't seem either easy or at all possible (without an exceedingly complicated process), and by all accounts isn't really required.
My question is then: What is the most efficient way of construction a custom component in java that passes or sets properties that would have otherwise been set by the attrs object when inflating a component using xml?
(Full disclosure: This question is an offshoot of Creating custom view)
You can create constructors beyond the three standard ones inherited from
View
that add the attributes you want......but I don't recommend it. It's better to follow the same convention as other components. This will make your component as flexible as possible and will prevent developers using your component from tearing their hair out because yours is inconsistent with everything else:
1. Provide getters and setters for each of the attributes:
2. Define the attributes in
res/values/attrs.xml
so they can be used in XML.3. Provide the three standard constructors from
View
.If you need to pick anything out of the attributes in one of the constructors that takes an
AttributeSet
, you can do...With all that done, you can instantiate
MyCompnent
programmatically......or via XML:
Additional Resource - https://developer.android.com/training/custom-views/create-view