Android Custom Views Level 2 AttributeSet
In the Previous Level, We have discussed a lot of basic terms View, ViewGroups, View Class Constructors, Canvas, and Paint. We have created our first custom view. check out cool exception InflateException. To Revise or Read Please follow the below link:
If you want to know about view Lifecycle and how it works in the Android Environment. I should suggest you go to the next chapter of this series.
In the previous section of the Custom View Blog series, we left a fight against the Second Constructor of View Class. Now, let's complete our Remaining Fight Against Second Constructor of View Class.
What we know till now
Integrating Custom view in XML requires the above constructor(we need to override parent’s two arguments constructor).
if we don’t add it, we will get inflate exception. So, we need attributeSet in the constructor to inflate view in XML.
What is AttributeSet?
A Collection of attributes or sets of attributes. If you are trying to create a custom view, and you want to pass in values like dimensions, colors, etc, you can do so with AttributeSet
.
What are the attributes?
Attributes are XML elements through which we can set properties of the custom view and control its appearance.
Steps to Define and Enable Properties of Attributes in Custom Views
1. Define custom attributes(Shape, size, colors, etc) for Custom view in a <declare-styleable>
resource element
2. Specify values for the attributes in your XML layout
3. Retrieve attribute values at runtime
4. Apply the retrieved attribute values to your view
Let us roll our shelves and do something Crazy Stuff
Goals:
1. Add some attribute in our circle View,
2. place our circle in the center of the screen using attribute onCenter
3. define the color of stroke using circle_stroke_color
4. circle radius using circle_radius
let us go
First, create attrs.xml resource file in resource/values folder

Second add <declare-styleable> and define attributes there
Define attributes in your XML
Modify our circle Custom View Class
TypedArray: An Array Container which holds value retrieved from resources. Be sure to call recycle()
when done with them.
get TypedArray (android.content.res.TypedArray) of attributes using obtainStyledAttributes(). Then we get our attributes using key and value pairs.
Final Code class
How many types of attributes?
we can add multiple types of attributes in custom view Like
reference — if it references another resource id (e.g, “@color/my_color”, “@layout/my_layout”), color, boolean, dimension, float, integer, string, fraction, enum — normally implicitly defined, flag — normally implicitly defined.
What we have done so far …. Summary Time
1. for using custom attributes we need to make File named attr in the values folder of the resource.
2. define your custom attributes in <declare-styleable> tag. we can define attributes as a KEY and Value which is the name and its type respectively.
3. modify properties of your custom view using attributes that we define in the attr file.
4. get them using TypedArray in the constructor of your class.
5. get attributes using different Get methods. it also provides us a way to provide a default value.
Yes, we have added custom attributes in my Custom CircleView. We are finished with the little demo on attributes. We have two more remaining constructors of the view class. but before that, we are going to dive into one of the important topics of Customs Views that is LifeCycle of View. LifeCycle is an interesting and tricky concept of the view class. So we have completed the demo on two constructors of the view class. we have made a custom circle view. we have done a lot in this session. Get read to view LifeCycle in the next blog… see you there.