Creating custom views
Now that we have created a simple user interface using views and ViewGroups, both using a layout resource and programmatically using Java, we will now have a go at creating our own custom views. Although the Android SDK provides many useful views, as we reviewed at the start of this chapter, sometimes you will find yourself extending the View
class and making your own to add additional functionality. Luckily, this is all supported out of the box and is pretty simple.
The first custom view example
In our first custom view example, we will extend the View
class and add some basic additional drawing code to the view. To begin, add a new Java class named CustomView
to your sample project. To add a new Java class, right-click on the com.example
folder located under app/java
, where SampleActivity
is currently located, and navigate to New |
Java Class. This will show the new class pop up as shown in the following screenshot:
Once you have created your class...