Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The Android Developer Guide has a section called <a href="http://developer.android.com/guide/topics/ui/custom-components.html" rel="noreferrer">Building Custom Components</a>. Unfortunately, <a href="http://developer.android.com/guide/topics/ui/custom-components.html#modifying" rel="noreferrer">the discussion of XML attributes</a> only covers declaring the control inside the layout file and not actually handling the values inside the class initialisation. The steps are as follows:</p> <h3>1. Declare attributes in <code>values\attrs.xml</code></h3> <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;resources&gt; &lt;declare-styleable name="MyCustomView"&gt; &lt;attr name="android:text"/&gt; &lt;attr name="android:textColor"/&gt; &lt;attr name="extraInformation" format="string" /&gt; &lt;/declare-styleable&gt; &lt;/resources&gt; </code></pre> <p>Notice the use of an unqualified name in the <code>declare-styleable</code> tag. Non-standard android attributes like <code>extraInformation</code> need to have their type declared. Tags declared in the superclass will be available in subclasses without having to be redeclared.</p> <h3>2. Create constructors</h3> <p>Since there are two constructors that use an <code>AttributeSet</code> for initialisation, it is convenient to create a separate initialisation method for the constructors to call. </p> <pre class="lang-java prettyprint-override"><code>private void init(AttributeSet attrs) { TypedArray a=getContext().obtainStyledAttributes( attrs, R.styleable.MyCustomView); //Use a Log.i("test",a.getString( R.styleable.MyCustomView_android_text)); Log.i("test",""+a.getColor( R.styleable.MyCustomView_android_textColor, Color.BLACK)); Log.i("test",a.getString( R.styleable.MyCustomView_extraInformation)); //Don't forget this a.recycle(); } </code></pre> <p><code>R.styleable.MyCustomView</code> is an autogenerated <code>int[]</code> 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, <code>R.styleable.MyCustomView_android_text</code> contains the <code>android_text</code> attribute for <code>MyCustomView</code>. Attributes can then be retrieved from the <code>TypedArray</code> using various <code>get</code> functions. If the attribute is not defined in the defined in the XML, then <code>null</code> is returned. Except, of course, if the return type is a primitive, in which case the second argument is returned.</p> <p>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 <code>android.R.attr</code>, while attributes for this project are in <code>R.attr</code>.</p> <pre class="lang-java prettyprint-override"><code>int attrsWanted[]=new int[]{android.R.attr.text, R.attr.textColor}; </code></pre> <p>Please note that you should <strong>not</strong> use anything in <code>android.R.styleable</code>, as per <a href="http://groups.google.com/group/android-developers/tree/browse_frm/thread/6554c6688f3ca6d9/8d018aa3f5c2beb9?rnum=1&amp;_done=%2Fgroup%2Fandroid-developers%2Fbrowse_frm%2Fthread%2F6554c6688f3ca6d9%3Ftvc%3D1%26#doc_72f29c1df624bbed" rel="noreferrer">this thread</a> 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.</p> <h3>3. Use it in a layout files such as <code>layout\main.xml</code></h3> <p>Include the namespace declaration <code>xmlns:app="http://schemas.android.com/apk/res-auto"</code> 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 <a href="https://stackoverflow.com/questions/128389/what-are-xml-namespaces-forhttps://stackoverflow.com/questions/128389/what-are-xml-namespaces-for">this article</a> for more info). The URL is simply a manner of uniquely identifying schemas - <a href="https://stackoverflow.com/questions/5758041/why-are-urls-in-xml-namespaces">nothing actually needs to be hosted at that URL</a>. 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.</p> <pre class="lang-xml prettyprint-override"><code>&lt;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" /&gt; </code></pre> <p>Reference the custom view using the fully qualified name.</p> <h3>Android LabelView Sample</h3> <p>If you want a complete example, look at the android label view sample.</p> <p><a href="http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/LabelView.html" rel="noreferrer">LabelView.java</a></p> <pre class="lang-java prettyprint-override"><code> TypedArray a=context.obtainStyledAttributes(attrs, R.styleable.LabelView); CharSequences=a.getString(R.styleable.LabelView_text); </code></pre> <p><a href="http://developer.android.com/resources/samples/ApiDemos/res/values/attrs.html" rel="noreferrer">attrs.xml</a></p> <pre class="lang-xml prettyprint-override"><code>&lt;declare-styleable name="LabelView"&gt; &lt;attr name="text"format="string"/&gt; &lt;attr name="textColor"format="color"/&gt; &lt;attr name="textSize"format="dimension"/&gt; &lt;/declare-styleable&gt; </code></pre> <p><a href="http://developer.android.com/resources/samples/ApiDemos/res/layout/custom_view_1.html" rel="noreferrer">custom_view_1.xml</a></p> <pre><code>&lt;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"/&gt; </code></pre> <p>This is contained in a <code>LinearLayout</code> with a namespace attribute: <code>xmlns:app="http://schemas.android.com/apk/res-auto"</code></p> <h3>Links</h3> <ul> <li><a href="https://stackoverflow.com/questions/2460074/how-to-retrieve-xml-attribute-for-custom-control">StackOverflow Thread: Retrieving an XML attribute for custom control</a></li> <li><a href="https://stackoverflow.com/questions/2127177/how-do-i-use-obtainstyledattributesint-with-internal-themes-of-android">How do I use obtainStyledAttributes with internal themes of Android</a></li> <li><a href="https://stackoverflow.com/questions/3441396/defining-custom-attrs">Defining custom attributes + list of supported attribute formats</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload