There
is an even easier option for creating a custom View which is useful in
certain circumstances. If there is a component that is already very
similar to what you want, you can simply extend that component and just
override the behavior that you want to change. You can do all of the
things you would do with a fully customized component, but by starting
with a more specialized class in the View hierarchy, you can also get a
lot of behavior for free that probably does exactly what you want.
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 DefinitionThe 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 asNoteEditor.MyEditTextfrom outside of theNoteEditorclass if desired. - It is
static, meaning it does not generate the so-called "synthetic methods" that allow it to access data from the parent class, which in turn means that it really behaves as a separate class rather than something strongly related toNoteEditor. This is a cleaner way to create inner classes if they do not need access to state from the outer class, keeps the generated class small, and allows it to be used easily from other classes. - It extends
EditText, which is the View we have chosen to customize in this case. When we are finished, the new class will be able to substitute for a normalEditTextview.
- It is defined as an inner class within the
- Class InitializationAs always, the super is called first. Furthermore, this is not a default constructor, but a parameterized one. The EditText is created with these parameters when it is inflated from an XML layout file, thus, our constructor needs to both take them and pass them to the superclass constructor as well.
- Overridden MethodsIn 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 theonDraw()method allows us to paint the blue lines on theEditTextview canvas (the canvas is passed into the overriddenonDraw()method). The super.onDraw() method is called before the method ends. The superclass method should be invoked, but in this case, we do it at the end after we have painted the lines we want to include. - Use the Custom ComponentWe
now have our custom component, but how can we use it? In the NotePad
example, the custom component is used directly from the declarative
layout, so take a look at
note_editor.xmlin theres/layoutfolder.<view class="com.android.notepad.NoteEditor$MyEditText" id="@+id/note" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@android:drawable/empty" android:padding="10dip" android:scrollbars="vertical" android:fadingEdge="vertical"/>
- 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$MyEditTextnotation which is a standard way to refer to inner classes in the Java programming language.If your custom View component is not defined as an inner class, then you can, alternatively, declare the View component with the XML element name, and exclude theclassattribute. For example:<com.android.notepad.MyEditText id="@+id/note" ... />
Notice that theMyEditTextclass is now a separate class file. When the class is nested in theNoteEditorclass, this technique will not work. - The other attributes and parameters in the definition are the ones passed into the custom component constructor, and then passed through to the EditText constructor, so they are the same parameters that you would use for an EditText view. Note that it is possible to add your own parameters as well, and we will touch on this again below.
- 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
And
that's all there is to it. Admittedly this is a simple case, but that's
the point — creating custom components is only as complicated as you
need it to be.
A more sophisticated component may override even more
on...
methods and introduce some of its own helper methods, substantially
customizing its properties and behavior. The only limit is your
imagination and what you need the component to do.
Attend Free Demo before joining the
course.
Gain hands on Real time project work Experience from
experienced professionals to land into IT jobs
Quontra Solutions
Email: info@quontrasolutions.com
Call Us:
US: +1 (404)-900-9988.
UK: (20) 3734 1498.
US: +1 (404)-900-9988.
UK: (20) 3734 1498.
CCNA training, CCNA
online training, CCNA online course, CCNA course, CCNA tutorials, CCNA learning
for beginners, CCNA training classes, CCNA class, CCNA videos
No comments:
Post a Comment