How do I declare an Android UI element using XML?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The Android Developer Guide has a section called Building Custom Components. Unfortunately, the discussion of XML attributes only covers declaring the control inside the layout file and not actually handling the values inside the class initialisation. The steps are as follows:
1. Declare attributes in
values\attrs.xmlNotice the use of an unqualified name in the
declare-styleabletag. Non-standard android attributes likeextraInformationneed to have their type declared. Tags declared in the superclass will be available in subclasses without having to be redeclared.2. Create constructors
Since there are two constructors that use an
AttributeSetfor initialisation, it is convenient to create a separate initialisation method for the constructors to call.R.styleable.MyCustomViewis an autogeneratedint[]resource where each element is the ID of an attribute. Attributes are generated for each property in the XML by appending the attribute name to the element name. For example,R.styleable.MyCustomView_android_textcontains theandroid_textattribute forMyCustomView. Attributes can then be retrieved from theTypedArrayusing variousgetfunctions. If the attribute is not defined in the defined in the XML, thennullis returned. Except, of course, if the return type is a primitive, in which case the second argument is returned.If you don’t want to retrieve all of the attributes, it is possible to create this array manually.The ID for standard android attributes are included in
android.R.attr, while attributes for this project are inR.attr.Please note that you should not use anything in
android.R.styleable, as per this thread it may change in the future. It is still in the documentation as being to view all these constants in the one place is useful.3. Use it in a layout files such as
layout\main.xmlInclude the namespace declaration
xmlns:app="http://schemas.android.com/apk/res-auto"in the top level xml element. Namespaces provide a method to avoid the conflicts that sometimes occur when different schemas use the same element names (see this article for more info). The URL is simply a manner of uniquely identifying schemas – nothing actually needs to be hosted at that URL. If this doesn’t appear to be doing anything, it is because you don’t actually need to add the namespace prefix unless you need to resolve a conflict.Reference the custom view using the fully qualified name.
Android LabelView Sample
If you want a complete example, look at the android label view sample.
LabelView.java
attrs.xml
custom_view_1.xml
This is contained in a
LinearLayoutwith a namespace attribute:xmlns:app="http://schemas.android.com/apk/res-auto"Links