Usage of TypedArray in Android Application
<p>I came across code at </p>

<h1>HelloGallery Example</h1>

<p><strong>ImageAdapter.java</strong> - <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>

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

<hr>

<p><strong>attrs.xml</strong> - <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>

<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>and also code at :</p>

<h1>Snake Game Example</h1>

<p><strong>TileView.java</strong> - <a href="http://developer.android.com/resources/samples/Snake/src/com/example/android/snake/TileView.html" rel="nofollow">http://developer.android.com/resources/samples/Snake/src/com/example/android/snake/TileView.html</a></p>

<pre><code>TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TileView);
mTileSize = a.getInt(R.styleable.TileView_tileSize, 12);
a.recycle();
</code></pre>

<hr>

<p><strong>attrs.html</strong> - <a href="http://developer.android.com/resources/samples/Snake/res/values/attrs.html" rel="nofollow">http://developer.android.com/resources/samples/Snake/res/values/attrs.html</a></p>

<pre><code>&lt;resources&gt;
  &lt;declare-styleable name="TileView"&gt;
    &lt;attr name="tileSize" format="integer" /&gt;
  &lt;/declare-styleable&gt;
&lt;/resources&gt;
</code></pre>

<hr>

<ol>
<li>May I know why they need to get the integer value from XML? Why don't they just code <code>mGalleryItemBackground = 0;</code> and <code>mTileSize = 12;</code>? My guess is that, they want to able to change something without touching Java code. But, I do not see any value being specified explicitly in the XML file itself. <strong>A code example to demonstrate the purpose of TypedArray and context.obtainStyledAttributes is very much appreicated.</strong></li>
<li>Both are trying to read an integer. Why one of the example is using <code>getResourceId</code> technique, another is using <code>getInt</code> technique?</li>
<li>I refer to <a href="http://developer.android.com/reference/android/content/res/TypedArray.html" rel="nofollow">TypedArray JavaDoc</a>, but I can hardly understand what <code>recycle</code> does?</li>
</ol>

<blockquote>
  <p>Give back a previously retrieved
  StyledAttributes, for later re-use.</p>
</blockquote>
