Not only can we define widgets in XML, set their properties, and generally manipulate and edit them at runtime, but it is also possible to create widgets entirely from scratch by extending existing views (or subclasses) and overriding their methods.
We are going to create a custom widget solely from within Java, so start a new Android project in Eclipse and open up the main Java activity.
Inside our new activity, add a new inner class called
MyCustomView
that extends the View class. It is always a good idea to get into the habit of declaring instances as static where possible, because memory leaks on a battery-operated device have a far greater impact than they do on a PC:private static class MyCustomView extends View { }
Give the new class a constructor:
public MyCustomView(Context context) { super(context); }
Now add and initialize a Paint field to this new class:
private static class MyCustomView extends View { final Paint paint...