The Android Developers Guide has a section called โCreating custom components.โ Unfortunately, the discussion of XML attributes only covers the declaration of the control inside the layout file, and not the processing of values โโinside the class initialization. These steps are as follows:
Declare attributes in \ attrs.xml values
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MyCustomView"> <attr name="android:text"/> <attr name="android:textColor"/> <attr name="extraInformation" format="string" /> </declare-styleable> </resources>
Note the use of an unqualified name in the declare-styleable tag. Custom Android attributes, such as extraInformation, must be of the declaration type. Tags declared in a superclass will be available in subclasses without the need to update them.
Create Constructors
Since there are two constructors that use AttributeSet initialization, it is convenient to create a separate initialization method for the called constructors.
private void init(AttributeSet attrs){ TypedArray a=getContext().obtainStyledAttributes(attrs,R.styleable.MyCustomView);
R.styleable.MyCustomView is an autogenerated int [] resource, where each element is an attribute identifier. Attributes are generated for each property in XML, adding the attribute name to the element name. Attributes can then be retrieved from TypedArray using various get functions. If the attribute is not defined in XML, null is returned. Except, of course, if the return type is primitive, then the second argument is returned.
If you do not want to retrieve all the attributes, you can create this array manually. The Android standard attribute identifier is included in android.R.attr, while the attributes for this project are in R.attr.
int attrsWanted[]=new int[]{android.R.attr.text, R.attr.textColor};
Please note that you should not use anything in the android.R.styleable file, as this may change in the future. The documentation still has a view of all these constants in one place.
Use it in layout files such as layout \ main.xml Include namespace declaration
XMLNS: application = "http://schemas.android.com/apk/res/com.mycompany.projectname"
in the top level xml element.
<com.mycompany.projectname.MyCustomView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/transparent" android:text="Test text" android:textColor="#FFFFFF" app:extraInformation="My extra information"; />
Link to user view using full name.
LabelView Example for Android
If you want a complete example, take a look at the android label presentation sample.
LabelView.java
TypedArray a=context.obtainStyledAttributes(attrs, R.styleable.LabelView); CharSequences=a.getString(R.styleable.LabelView_text); attrs.xml <declare-styleable name="LabelView"> <attr name="text"format="string"/> <attr name="textColor"format="color"/> <attr name="textSize"format="dimension"/> </declare-styleable>
custom_view_1.xml
<com.example.android.apis.view.LabelView android:background="@drawable/blue" android:layout_width="fill_parent" android:layout_height="wrap_content" app:text="Blue"app:textSize="20dp"/>
This is contained in a LinearLayout with a namespace attribute:
XMLNS: application = "http://schemas.android.com/apk/res/com.example.android.apis"