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.
- 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.
To summarize, the use of a Layout as the basis for a Custom Control has a number of advantages, including:
- You can specify the layout using the declarative XML files just like with an activity screen, or you can create views programmatically and nest them into the layout from your code.
- The
onDraw()andonMeasure()methods (plus most of the otheron...methods) will likely have suitable behavior so you don't have to override them. - In the end, you can very quickly construct arbitrarily complex compound views and re-use them as if they were a single component.
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.
No comments:
Post a Comment