text: .
  If. re-assign Customized Components
  Fully customized components can be used to create graphical components that appear however you wish. Perhaps a graphical VU meter that looks like an old analog gauge, or a sing-a-long text view where a bouncing ball moves along the words so you can sing along with a karaoke machine. Either way, you want something that the built-in components just won't do, no matter how you combine them.
  Fortunately, you can easily create components that look and behave in any way you like, limited perhaps only by your imagination, the size of the screen, and the available processing power (remember that ultimately your application might have to run on something with significantly less power than your desktop workstation).
  To create a fully customized component:
  - well.
  - You will almost certainly want to override
  onMeasure()and are also likely to need to override
  onDraw()if you want the component to show something. While both have default behavior, the default
  onDraw()will do nothing, and the default
  onMeasure()will always set a size of 100x100 — which is probably not what you want.
  - Other
  on...methods may also be overridden as required.
  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.
  onMeasure() is a little more involved.
  onMeasure()
  is a critical piece of the rendering contract between your component and its
  container.
  onMeasure() should be overridden to efficiently and
  accurately report the measurements of its contained parts. This is made
  slightly more complex by the requirements of limits from the parent
  (which are passed in to the
  onMeasure() method) and by the
  requirement to call the
  setMeasuredDimension() method with the
  measured width and height once they have been calculated. If you fail to
  call this method from an overridden
  onMeasure() method, the
  result will be an exception at measurement time.
  At a high level, implementing
  onMeasure() looks something
  like this:
  - The overridden
  onMeasure()method is called with width and height measure specifications (
  widthMeasureSpecand
  heightMeasureSpecparameters, 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 under
  View.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 the
  onMeasure()to try again, perhaps with different measurement specifications).
  -:
  A Custom View Example
  The CustomView sample in the API Demos provides an example of a customized View. The custom View is defined in the LabelView class.
  The LabelView sample demonstrates a number of different aspects of custom components:
  - Extending the View class for a completely custom component.
  - Parameterized constructor that takes the view inflation parameters (parameters defined in the XML). Some of these are passed through to the View superclass, but more importantly, there are some custom attributes defined and used for LabelView.
  - Standard public methods of the type you would expect to see for a label component, for example
  setText(),
  setTextSize(),
  setTextColor()and so on.
  - An overridden
  onMeasuremethod to determine and set the rendering size of the component. (Note that in LabelView, the real work is done by a private
  measureWidth()method.)
  - An overridden
  onDraw()method to draw the label onto the provided canvas.
  You can see some sample usages of the LabelView custom View in
  custom_view_1.xml
  from the samples. In particular, you can see a mix of both
  android:
  namespace parameters and custom
  app: namespace parameters. These
  app: parameters are the custom ones that the LabelView recognizes
  and works with, and are defined in a styleable inner class inside of the
  samples R resources definition class.
  Compound Controls
  If you don't want to create a completely customized component, but instead are looking to put together a reusable component that consists of a group of existing controls, then creating a Compound Component (or Compound Control) might fit the bill. In a nutshell, this brings together a number of more atomic controls (or views) into a logical group of items that can be treated as a single thing. For example, a Combo Box can be thought of as a combination of a single line EditText field and an adjacent button with an attached PopupList. If you press the button and select something from the list, it populates the EditText field, but the user can also type something directly into the EditText if they prefer.
  In Android, there are actually two other Views readily available to do
  this:
  Spinner and
  AutoCompleteTextView, but
  regardless, the concept of a Combo Box makes an easy-to-understand
  example.
  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.
  - might override other
  on...methods, like
  onKeyDown(), to perhaps choose certain default values from the popup list of a combo box when a certain key is pressed..java..
  For example, the SDK includes a NotePad application in the samples. This demonstrates many aspects of using the Android platform, among them is extending an EditText View to make a lined notepad. This is not a perfect example, and the APIs for doing this might change from this early preview, but it does demonstrate the principles.
  If you haven't done so already, import the
  NotePad sample into Eclipse (or
  just look at the source using the link provided). In particular look at the definition of
  MyEditText in the NoteEditor.java
  file.
  Some points to note here
  - The Definition
  The class is defined with the following line:
  public static class MyEditText extends EditText
  - It is defined as an inner class within the
  NoteEditoractivity, but it is public so that it could be accessed as
  NoteEditor.My
  In this example, there is only one method to be overridden:
  onDraw()— but there could easily be others needed when you create your own custom components.
  For the NotePad sample, overriding the
  onDraw()method allows us to paint the blue lines on the
  EditTextview canvas (the canvas is passed into the overridden
  onDraw()method). The super.onDraw() method is called before the method ends. The superclass method should be invoked,folder.
  <view class="com.android.notepad.NoteEditor$MyEditText" id="@+id/note" android:
  - The custom component is created as a generic view in the XML, and the class is specified using the full package. Note also that the inner class we defined is referenced using the
  NoteEditor$My.android.notepad.MyEditText id="@+id/note" ... />
  Notice that the
  My.