Reading custom attributes

satya - Wed Oct 24 2012 17:36:52 GMT-0400 (Eastern Daylight Time)

obtainStyledAttributes API

obtainStyledAttributes API

satya - Wed Oct 24 2012 17:37:28 GMT-0400 (Eastern Daylight Time)

My previous article on custom attributes

My previous article on custom attributes

satya - Wed Oct 24 2012 17:43:05 GMT-0400 (Eastern Daylight Time)

Here are constructors that read custom attributes


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

satya - Wed Oct 24 2012 17:46:29 GMT-0400 (Eastern Daylight Time)

The last constructor is saying

Get it from the attribute set. If an attribute is not found, look in the current theme for a set of attributes identified the style name or id "defStyle". If it is not there let it go. A class could supply its own style resource as the fourth argument (we used 0 there).

Remember a style resource is essentially a collection of attributes and their values.

satya - Wed Oct 24 2012 17:47:12 GMT-0400 (Eastern Daylight Time)

Here is the attrs.xml for this


<resources>
<declare-styleable name="CircleView">
    <attr name="strokeWidth" format="integer"/>
    <attr name="strokeColor" format="color|reference" />
</declare-styleable>
</resources>

satya - Wed Oct 24 2012 17:48:04 GMT-0400 (Eastern Daylight Time)

Here is the layout definition


<?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"    
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    >
...
<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" 
    />    
....
</LinearLayout>

satya - Wed Oct 31 2012 15:45:29 GMT-0400 (Eastern Daylight Time)

Example of enumerations


<resources>
<declare-styleable name="CircleView">
    <attr name="strokeWidth" format="integer"/>
    <attr name="strokeColor" format="color|reference" />
</declare-styleable>
<declare-styleable name="DurationComponent">
    <attr name="durationUnits">
      <enum name="days" value="1"/>
      <enum name="weeks" value="2"/>
    </attr>
</declare-styleable>
</resources>

satya - Wed Oct 31 2012 15:48:18 GMT-0400 (Eastern Daylight Time)

Why ids generated in R.java for enumerations in attrs.xml

Why ids generated in R.java for enumerations in attrs.xml

Search for: Why ids generated in R.java for enumerations in attrs.xml