title: Dynamically adding a child to LinearLayout with getting each child's position
body: <p>I have a problem with getting the child's position of <em>LinearLayout</em>. First I'm adding dynamically a number of buttons and then I'm trying to return each child's index and display it into a <em>TextView</em>. Here I'm sharing code:</p>
  
  <p><strong>java source</strong>:</p>
  
  <pre><code>private String[] categories;
  
  private LinearLayout ll;
  private TextView tv;
  
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  
      categories = getResources().getStringArray(R.array.categories);
  
      tv = (TextView) findViewById(R.id.text);
      ll = (LinearLayout) findViewById(R.id.hsvLinearLayout);
  
      for(int i = 0; i &lt; categories.length; i++) {
          Button btn = new Button(this);
          btn.setText(categories[i]);
          btn.setOnClickListener(buttonClick);
          ll.addView(btn);
      }
  }
  
  OnClickListener buttonClick = new OnClickListener() {
      public void onClick(View v) {
          tv.setText(ll.indexOfChild(v));
      }
  };
  </code></pre>
  
  <p><strong>xml structure</strong>:</p>
  
  <pre><code>&lt;LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  &gt;
  
  &lt;HorizontalScrollView
      android:id="@+id/Footer"
      android:orientation="horizontal"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:scrollbars="none"
      android:fadingEdge="none"
      &gt;
  
      &lt;LinearLayout
          android:id="@+id/hsvLinearLayout"
          android:orientation="horizontal"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          &gt;
  
      &lt;/LinearLayout&gt;
  
  &lt;/HorizontalScrollView&gt;
  
  &lt;TextView
      android:id="@+id/text"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      /&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>resources</strong>:</p>
  
  <pre><code>&lt;string name="today"&gt;Today&lt;/string&gt;
  &lt;string name="life"&gt;Life&lt;/string&gt;
  &lt;string name="corner"&gt;Corner&lt;/string&gt;
  &lt;string name="banks"&gt;Banks&lt;/string&gt;
  &lt;string name="it"&gt;IT&lt;/string&gt;
  &lt;string name="fun"&gt;Fun&lt;/string&gt;
  
  &lt;array name="categories"&gt;
      &lt;item&gt;@string/today&lt;/item&gt;
      &lt;item&gt;@string/life&lt;/item&gt;
      &lt;item&gt;@string/corner&lt;/item&gt;
      &lt;item&gt;@string/banks&lt;/item&gt;
      &lt;item&gt;@string/it&lt;/item&gt;
      &lt;item&gt;@string/fun&lt;/item&gt;
  &lt;/array&gt;
  </code></pre>
  
  <p>The dynamically adding is fine, but the way I'm setting the <em>OnClickListener</em> produce some error. Any help will be useful! The purpose of that is if I want to add one or more button(s) to the <em>HorizontalScrollView</em> not to be necessary to edit many files, and just to go to <strong>sting.xml</strong> and create a new item into the <em>categories-array</em>!</p>
  
  <p>This is what <strong>LogCat</strong> produced:</p>
  
  <pre><code>07-12 22:37:17.680: INFO/System.out(331): waiting for debugger to settle...
  07-12 22:37:17.900: INFO/System.out(331): debugger has settled (1441)
  07-12 22:37:20.870: INFO/ActivityManager(61): Displayed activity com.test/.TestDynam: 10203 ms (total 10203 ms)
  07-12 22:37:25.552: WARN/ResourceType(331): No package identifier when getting value for resource number 0x00000000
  07-12 22:37:26.871: DEBUG/dalvikvm(132): GC freed 190 objects / 8976 bytes in 901ms
  </code></pre>
  
  <p>To clarify - the application starts without any errors, but when I click on a button it produces this line from the above log:</p>
  
  <pre><code>07-12 22:37:25.552: WARN/ResourceType(331): No package identifier when getting value for resource number 0x00000000
  </code></pre>
  
  <p>And this is from <strong>debugger</strong>:</p>
  
  <pre><code>TestDynam [Android Application] 
      DalvikVM[localhost:8611]    
          Thread [&lt;3&gt; main] (Suspended (exception Resources$NotFoundException))   
              ViewRoot.handleMessage(Message) line: 1704  
              ViewRoot(Handler).dispatchMessage(Message) line: 99 
              Looper.loop() line: 123 
              ActivityThread.main(String[]) line: 4203    
              Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
              Method.invoke(Object, Object...) line: 521  
              ZygoteInit$MethodAndArgsCaller.run() line: 791  
              ZygoteInit.main(String[]) line: 549 
              NativeStart.main(String[]) line: not available [native method]  
          Thread [&lt;13&gt; Binder Thread #2] (Running)    
          Thread [&lt;11&gt; Binder Thread #1] (Running)
  </code></pre>