Android Custom Views Part-3: Shape Manipulations

The easiest job while making a custom view is making its one-time static part, that is, the visible view when it is rendered for the first time. What comes next after you’ve added custom attributes, is making shape manipulations using developer-defined functions in the custom view class.

This story is a part of the whole series on how to make custom views. If you haven’t seen that Part 1 and 2, I would recommend going through them 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 make shape manipulations using developer-defined custom view functions. We will aim to create two functions: to provide shape manipulations onto the custom attributes (covered in Pt. 2) and one without the use of custom attributes.

We will proceed by following the mentioned steps:

  1. Making three buttons in activity_main.xml to : increase/decrease custom padding, and one to swap colour; we will also provide their onClicks onto the respective activity class.

2. Give your custom view an android id in the activity_main.xml to be referenced in the activity class. Create empty functions in MyCustomView.class and call them on respective onClicks of buttons in activity_main.xml. Also make a global variable mPadding that adds/subtracts to mRect dimensions accordingly.

3. Next step is to provide implementations for functions to make modifications, and then refresh the custom view by calling postInvalidate();

Thats it! now you have your custom views with functions that manipulate the way your view is displayed! With practice and innovation, you can very easily command over the whole procedures that followed.

Again, you can get the running project here:

Stay tuned for more Android related tutorials.

And as usual, Happy Programming. :)