Tuesday, May 1, 2012

Creating a Custom View That Takes Attributes

This tutorial will demonstrate how to create a custom view that will change its appearance based on a custom attribute set in the xml layout.


1. Custom Attributes

If you have used Android Views in the layout editor, you are probably already familiar with setting attributes for views. For example, for an ImageView you can set the drawable attribute to determine what graphic to draw, or the Text Attribute for a button or label to determine what text will display.

If you find yourself designing a custom view, you may want to add custom attributes to that view that affect the display or behavior. For this example we will build off of the custom view that created before, and add an attribute to change the border color.