what attributes are allowed for a custom object?
satya - 3/21/2013 9:42:24 AM
Consider a package: com.androidbook.custom
Consider a package: com.androidbook.custom
satya - 3/21/2013 9:43:32 AM
Consider a custom object in that package
com.androidbook.custom.CircleVliew
satya - 3/21/2013 9:44:00 AM
Consider the following attrbs.xml
<resources>
<declare-styleable name="CircleView">
<attr name="strokeWidth" format="integer"/>
<attr name="strokeColor" format="color|reference" />
<attr name="mycolor1" format="color|reference"/>
</declare-styleable>
<declare-styleable name="CircleView2">
<attr name="mycolor2" format="color|reference"/>
</declare-styleable>
</resources>
satya - 3/21/2013 9:45:36 AM
At this point the styleable names appear arbitrary
we may have named the first styleable as CircleView. But it is not checked against any name space.
Proof: CircleView2 doesnt exist as a class. It is just a name.
Further know that the attribute names are unique across the whole package even if they are under different stuleables.
satya - 3/21/2013 9:49:24 AM
Now consider this layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:apptemplate="http://schemas.android.com/apk/res/com.androidbook.custom"
>
<com.androidbook.custom.CircleView
android:id="@+id/circle_view_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
apptemplate:strokeWidth="5"
apptemplate:strokeColor="@android:color/holo_red_dark"
apptemplate:mycolor2="@android:color/holo_red_dark"
/>
</LinearLayout>
satya - 3/21/2013 9:56:29 AM
Couple of things to notice
The name space needs to be the name of the package.
As long as the class name "CircleView" matches the styleable "CircleView" the ADT tool will prompt you for the valid attributes that are allowed as children of "CircleView"
you can experiment by changing the name of the styleable "CircleView" to "CircleView3" and you will see that you will loose the prompting for children in the xml nodes.
However if you have a valid attribute, even under a different styleable, "mycolor2" you can put it in the xml of "CircleView" although it may not read it. Of course as stated earlier you won't be prompted for it. But it does not result in an error.
Potentially the implication is you can put any valid "android" attribute belonging to any component in this attribute set!!
satya - 3/21/2013 9:57:31 AM
Of course we may not be reading anything other than what is required
public CircleView(Context context) {
super(context);
initCircleView();
}
public CircleView(Context context, int inStrokeWidth, int inStrokeColor) {
super(context);
strokeColor = inStrokeColor;
strokeWidth = inStrokeWidth;
initCircleView();
}
public CircleView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
//Meant for derived classes to call
public CircleView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray t = context.obtainStyledAttributes(attrs,R.styleable.CircleView, defStyle,0);
strokeColor = t.getColor(R.styleable.CircleView_strokeColor, strokeColor);
strokeWidth = t.getInt(R.styleable.CircleView_strokeWidth, strokeWidth);
t.recycle();
initCircleView();
}