Android Custom Views Tutorial. Part-2: Custom Attributes

Robin Kamboj
Aug 8, 2017 · 3 min read

In the last part of this series, we discussed how to make basic Custom Shapes in Android. If you haven’t seen that Part, I would recommend going through that first:

If you want to get hold of the project code, it is recommended that you clone/fork/download the following repository:

In this part of the series, you will learn to add custom attributes to the Custom Views.

The previous part of the series has the code for MyCustomView as:

https://gist.github.com/robillo/a270fc6f5ce26176574cf6db149d9c9a/raw/57b46798155302a9e4d8b9cbbe7550a423a5ba3f/MyCustomView.java

In this code for the custom view, you may have noticed that we have the init() method with empty body, and several warnings are there in the onDraw() method that was overridden to avoid using layout allocations during draw operations. Removing this warning is very important, as this is one of the easiest causes of a memory leak. Our first step of this series will be to remove this warning only.

4. Next, we need to check in our init() method whether the AttributeSet set being passed as a parameter is null or not. If it is not null, then we obtain a TypedArray typedArray (say) by calling obtainStyledAttributes(set, R.styleable.MyCustomView) using getContext();

5. Next, we declare an int variable mSquareColor and initialise with the values input through the TypedArray ta, also providing the default colour in case no value for that attribute was input by the user. Also remember to call ta.recycle() once you are done accessing it. Your class should now look like this:

6. Now all you need to do is add your custom attribute square_color to your activity_main.xml , you will see that the custom view colour changes to whatever colour you add inside the attribute parameter.

Thats all you needed to know how you make custom attributes for your custom views. More examples on custom attributes are for size of your view, radius in case of circle, text input, etc.

To continue learning in depth of the basics of custom view, it is recommended that you go through the remaining parts of the series. And like always, happy programming. :)

If you liked the post, please press the little heart/follow to promote more tuts from this side. :)

Link to other parts of this series:

MindOrks

Our community publishes stories worth reading on software development and design. Android | Machine Learning | #MakeEveryoneCode

Robin Kamboj

Written by

Software Engineer by profession. Designer by force. Bibliophile by nature.

MindOrks

MindOrks

Our community publishes stories worth reading on software development and design. Android | Machine Learning | #MakeEveryoneCode

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade