Android Declare buttons programmatically?
<p>In my soundboard app I have 80 buttons that have a on click listener and a on long click listener. </p>

<p>My buttons are declared in xml as: </p>

<pre><code>&lt;TableRow
        android:id="@+id/tableRow1"
        android:layout_height="wrap_content" &gt;

        &lt;Button
            android:id="@+id/sound0"
            android:layout_width="1dip"
            android:layout_height="fill_parent"
            android:layout_weight=".31"
            android:longClickable="true"
            android:text="@string/sound0" &gt;
        &lt;/Button&gt;

        &lt;Button
            android:id="@+id/sound1"
            android:layout_width="1dip"
            android:layout_height="fill_parent"
            android:layout_weight=".31"
            android:longClickable="true"
            android:text="@string/sound1" &gt;
        &lt;/Button&gt;

        &lt;Button
            android:id="@+id/sound2"
            android:layout_width="1dip"
            android:layout_height="fill_parent"
            android:layout_weight=".31"
            android:longClickable="true"
            android:text="@string/sound2" &gt;
        &lt;/Button&gt;
    &lt;/TableRow&gt;
</code></pre>

<p>And the listeners are set as:</p>

<pre><code>Button SoundButton0 = (Button) findViewById(R.id.sound0);
    SoundButton0.getBackground().setAlpha(150);

    SoundButton0.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            String name = getString(R.string.sound0);
            tracker.trackEvent("Clicks", "Play", name, 0);
            playSound(R.raw.sound0);

        }
    });
    SoundButton0.setOnLongClickListener(new OnLongClickListener() {

        public boolean onLongClick(View v) {
            String name = getString(R.string.sound0);
            tracker.trackEvent("Clicks", "Saved", name, 0);
            ring(soundArray[0], name);
            return false;

        }
    });
</code></pre>

<p>Is there a way I can do all of this programmatically in a for loop so that the only thing changed for each button is SoundButtonx where x is increased by one for each button.</p>
