1

In my soundboard app I have 80 buttons that have a on click listener and a on long click listener.

My buttons are declared in xml as:

<TableRow
        android:id="@+id/tableRow1"
        android:layout_height="wrap_content" >

        <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" >
        </Button>

        <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" >
        </Button>

        <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" >
        </Button>
    </TableRow>

And the listeners are set as:

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;

        }
    });

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.

Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
mpeerman
  • 2,050
  • 2
  • 16
  • 16

3 Answers3

3

Yes there is a clear solution:

Button[] buttons; 
for(int i=0; i<buttons.length; i++) {
{
     String buttonID = "sound" + (i+1);

     int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
     buttons[i] = ((Button) findViewById(resID));
     buttons[i].setOnClickListener(this);
}

Note: Declare XML layout with buttons having id like sound1, sound2, sound3, sound4....and so on.

A more clear-cut example is here for the same problem => Android – findViewById() in a loop

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
0

Yes there is. Inside your for loop, you first declare a Button, and pass its constructor a context. Then you set each button's layout params. Add each button to the parent view (use the addView method on the parent view). Finally, use the setContentView method of the activity and pass the parent as a param.

Dhruv Gairola
  • 9,102
  • 5
  • 39
  • 43
0
yes take a look at this

for(int x = 0;x<80;x++)
{
Button btn = new Button(this);
btn.setlayoutParams(new LayoutParams(LayoutParams.wrap_content,LayoutParams.wrap_conmtent);
btn.setId(100 + x);
btn.setOnClickListener(this);
btn.setOnlongClickListener(this);
this.addView(btn);
}

//this will create 80 buttons and setlisteners on them

//in your overrides of onclick and onLongClick identiffy them as 

    public void onClick(View v) {
        // TODO Auto-generated method stub
        int id = v.getId();
    if(id == 100 + 1 )
    {
//your code
    }
vipin
  • 2,851
  • 4
  • 18
  • 32