Android Custom Controls Basics

January 17, 2015 - Android / Java

This small tutorial shows the basic steps to create a custom GUI control in Android.

A Custom Control Skeleton

Every GUI control is a descendant of android.view.View. The example below contains the most common elements necessary to build up an Android Custom Control.

  • Constructors

At least one constructor receiving a Context parameter must me implemented and it must call the parent constructor with it.

For providing XML file configuration to the custom control, a constructor receiving an AttributeSet is necessary. This constructor should parse the AttributeSet to get the configuration parameters defined for the instance; this is done using a TypedArray object, as shown above.

  • onDraw()

The custom control is drawn into the user Activity or Fragment by the method onDraw().

onDraw() takes a Canvas parameter, that must be used to draw itself. The default onDraw() implementation only paints a rectangle filled with the background color; you can call the inherited method before drawing, so there is no need to erase the background.

onDraw() must be implemented efficiently and return quickly. New objects should not be created inside it, instead create any supporting objects during initialization and reuse them in onDraw(). For example, almost always a Paint object is needed: it should be created during initialization, for example in the do_init() method.

  • onMeasure()

The onMeasure() method is called before drawing an object, to specify its size. A different implementation may be necessary if the custom object behaves differently depending on the final size.

onMeasure() must call the method setMeasuredDimension() to define its dimensions or instead call the parent method.

Note that its parameters are suggestions from Android OS for the object size; but when their specification is “EXACTLY” the value should not be changed, having to be used to set the object size by calling setMeasuredDimension().

  • onTouchEvent()onKeyUp()onKeyDown(), etc

Those methods should be overridden to define the control’s behavior during user interaction, as the user touches the object, press or release a key while the object has the focus, etc.

After processing the user event, the parent method may be called or the event dispatched for the programmer, for example by calling performClick().

Usually the interaction with the user causes the object to change its appearance; in this case, a redraw must be requested by calling the invalidate() method, which will schedule another call to onDraw().

XML Object Properties

Your custom control can use XML-based configuration, just like any other Android control, for example supporting a GUI interface designer, such as Eclipse or Android Studio.

For declaring properties, use a resource of type <declare-styleable> in the project, into a XML values file, like attrs.xml :

Once declared as such, the custom control can be added into a GUI editor (it shall be found in “Custom & Library Views”) and its properties can be edited by the property editor. The control can be declared into a XML layout, for example:

Those custom XML properties should be read by the constructor with an AttributeSet:

The “R” constants are automatically created by the compiler, based on the values specified into <declare-styleable>.

› tags: Android / GUI / Java /

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">