Android offers a sophisticated and powerful componentized model for building your UI, based on the fundamental layout classes: View
and ViewGroup
. To start with, the platform includes a variety of prebuilt View and ViewGroup subclasses — called widgets and layouts, respectively — that you can use to construct your UI.
》A partial list of available widgets includes Button
, TextView
,EditText
, ListView
, CheckBox
, RadioButton
, Gallery
,Spinner
, and the more special-purpose AutoCompleteTextView
,ImageSwitcher
, and TextSwitcher
.
》Extend onDraw()
and onMeasure()
The onDraw()
method delivers you a Canvas
upon which you can implement anything you want: 2D graphics, other standard or custom components, styled text, or anything else you can think of.
Note: This does not apply to 3D graphics. If you want to use 3D graphics, you must extend SurfaceView
instead of View, and draw from a separate thread. See the GLSurfaceViewActivity sample for details.
At a high level, implementing onMeasure()
looks something like this:
- The overridden
onMeasure()
method is called with width and height measure specifications (widthMeasureSpec
andheightMeasureSpec
parameters, both are integer codes representing dimensions) which should be treated as requirements for the restrictions on the width and height measurements you should produce. A full reference to the kind of restrictions these specifications can require can be found in the reference documentation underView.onMeasure(int, int)
(this reference documentation does a pretty good job of explaining the whole measurement operation as well). - Your component's
onMeasure()
method should calculate a measurement width and height which will be required to render the component. It should try to stay within the specifications passed in, although it can choose to exceed them (in this case, the parent can choose what to do, including clipping, scrolling, throwing an exception, or asking theonMeasure()
to try again, perhaps with different measurement specifications). - Once the width and height are calculated, the
setMeasuredDimension(int width, int height)
method must be called with the calculated measurements. Failure to do this will result in an exception being thrown.
To create a compound component:
- The usual starting point is a Layout of some kind, so create a class that extends a Layout. Perhaps in the case of a Combo box we might use a LinearLayout with horizontal orientation. Remember that other layouts can be nested inside, so the compound component can be arbitrarily complex and structured. Note that just like with an Activity, you can use either the declarative (XML-based) approach to creating the contained components, or you can nest them programmatically from your code.
- In the constructor for the new class, take whatever parameters the superclass expects, and pass them through to the superclass constructor first. Then you can set up the other views to use within your new component; this is where you would create the EditText field and the PopupList. Note that you also might introduce your own attributes and parameters into the XML that can be pulled out and used by your constructor.
- You can also create listeners for events that your contained views might generate, for example, a listener method for the List Item Click Listener to update the contents of the EditText if a list selection is made.
- You might also create your own properties with accessors and modifiers, for example, allow the EditText value to be set initially in the component and query for its contents when needed.
- In the case of extending a Layout, you don't need to override the
onDraw()
andonMeasure()
methods since the layout will have default behavior that will likely work just fine. However, you can still override them if you need to. - You might override other
on...
methods, likeonKeyDown()
, to perhaps choose certain default values from the popup list of a combo box when a certain key is pressed.