Why we need to use Context.obtainStyledAttributes and declare-styleable to obtain attribute of a GUI component
<p>I just came across some tutorial code example which are using <code>declare-styleable</code> in XML and <code>Context.obtainStyledAttributes</code> in Java code.</p>

<p>My understanding is, <em>it is trying to obtain the attribute (Like background color) of a GUI component.</em></p>

<p><strong>I was wondering, why do we need to go through these cumbersome steps just to obtain a simple attribute? If I want to retrieve a GUI component attributes each time, I have to create a new XML file, and add in the attribute I want to retrieve to the XML file itself. Can we have something simpler?</strong></p>

<p>Currently, here is what I need to do when I try to obtain attribute of a GUI component.</p>

<hr>

<p><a href="http://developer.android.com/resources/tutorials/views/hello-gallery.html" rel="nofollow">http://developer.android.com/resources/tutorials/views/hello-gallery.html</a></p>

<p>Create an XML file.</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;resources&gt;
    &lt;declare-styleable name="HelloGallery"&gt;
        &lt;attr name="android:galleryItemBackground" /&gt;
    &lt;/declare-styleable&gt;
&lt;/resources&gt;
</code></pre>

<p>Have the following Java code.</p>

<pre><code>TypedArray a = context.obtainStyledAttributes(R.styleable.HelloGallery);
mGalleryItemBackground = a.getResourceId(
        R.styleable.HelloGallery_android_galleryItemBackground, 0);
a.recycle();
</code></pre>

<hr>

<p>Can we have something simple like</p>

<pre><code>helloGalleryInstance.getBackground();
</code></pre>

<p>As in Swing, here is what I usually do to obtain an attribute of a GUI component. It is fun and easy. Can we have something similar in Android?</p>

<pre><code>instaceOfAComponent.getBackground();
</code></pre>
