what attributes are allowed for a custom object?

Consider a package: com.androidbook.custom


com.androidbook.custom.CircleVliew

<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>

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.


<?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>

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!!


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();
}