Sunday, July 13, 2014

Android : Compound Controls

If the Application requirement is to just group a certain already existing component and create it as a group, This is also possible in android and this is categorised as Compound Controls. 
There are already some components in System framework which does this. For e.g. Spinner, AutoCompleteTextView 

Below are the steps to create a CompoundControl 

1. The usual starting point is layout of some kind, so, create a class that extends the Layout. For e.g. LinearLayout. The layout can be nested inside to make complex compound components. Note that just like with an Activity, you can use either the declarative (XML based) approach to creating the contained components, or application can nest them programmatically in the code. 

2. In the constructor of the new class, take whatever parameters the superclass expects. and pass them through to the super contractor first. After this, the component component can set up other components those are readily available or other custom components. Note that application might also introduce own attributes and parameters into XML that can be pulled out and used by the new compound controls constructor. 

3. Compound controls can also have own listeners 

4. Compound controls can expose new properties and methods that may deem necessary for the functionality and usefulness of the component 

5. In case application is extending a layout, application don't need to override onDraw() or onMeasure methods since the layout will have default behavior that will likely just work fine. However, application can override it still would like to. 

6. The application can override other on… methods such as onKeyDown if required. 

There is a Compound component example given in the 

With this in mind, lets create a CardView similar to single card in Pinterest app. The card can have ImageView that contains the main image and Label Below it and a separator image, then an image in round shape for the profile picture and a label for description. The sample is List4.java and List6.java 

The List6.java class creates a SpeachView class as a compound class. This compound component is created programmatically. And holds two TextView components. 

The class is declared as extending the LinearLayout. Other methods such as onMeasure and onLayout are not overridden in this class, which means that it lets the system to layout the components within it. 

References: 


No comments:

Post a Comment