user1105975
user1105975

Reputation: 121

how to apply different onclick action to each button in array of buttons in android

hi i am doing one application here i need to disply 6 images in 3 colunmns and 3 rows.and then when i click each image i need to perform different onclick action.i teried some way using arraylist with forloop.using below code i applied onclick function into all images but here 2 cloumn 3 images onclick function working but i first column 3 images not working onclick function.but i need to apply different onclick action to each button. so please any one help me how to apply onclick action to array of images.

     game2 .class:
        public class game2 extends Activity implements OnClickListener {
    TableLayout layout;
          int i=0;
           int f=0;
                Integer[] images={R.drawable.abig,R.drawable.cbig,R.drawable.dbig,R.drawable.abig,R.drawable.cbig,R.drawable.dbig};
 List<Integer> solutionList = Arrays.asList(images);
    Integer[] randomNumbers,randomNumbers1;
TableLayout.LayoutParams param,param1;
    ImageView[] plus=new ImageView[6];
     TableRow[] row = new TableRow[6];
     RelativeLayout.LayoutParams lp2,lp1,lp3,lp4,lp5;
      RelativeLayout linear;
     @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main1);
         layout = new TableLayout (this);
         layout.setLayoutParams( new TableLayout.LayoutParams(40,50) );
         param=new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT);
         param.setMargins(25, 0, 0, 0);
         lp1=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
         linear=(RelativeLayout)findViewById(R.id.linear);
         Collections.shuffle(solutionList);
            randomNumbers = (Integer[])solutionList.toArray();
              int unique=0;
               for (f=0; f<3; f++) {
             row[f] = new TableRow(this);
             for (int c=0; c<2; c++) {
                 plus[f]=new ImageView(this);
                   plus[f].setBackgroundResource(randomNumbers[unique]);
                   plus[f].setOnClickListener(this);
                    row[f].addView(plus[f], 200,100);
                    unique++;
             } // for
             layout.addView(row[f],param);
         } // for

         linear.addView(layout,lp1);
         setContentView(linear);
        }

      public void onClick(View view) {
           if(view==plus[0])
            {

                  Toast.makeText(game2.this, "view", Toast.LENGTH_SHORT).show();
            }
            if(view==plus[1])
            {
                  Toast.makeText(game2.this, "view1", Toast.LENGTH_SHORT).show();
            }
            if(view==plus[2])
            {
                  Toast.makeText(game2.this, "view2", Toast.LENGTH_SHORT).show();
            }
            if(view==plus[3])
            {
                  Toast.makeText(game2.this, "view3", Toast.LENGTH_SHORT).show();   
            }
            if(view==plus[4])
            {
                  Toast.makeText(game2.this, "view4", Toast.LENGTH_SHORT).show();
            }
            if(view==plus[5])
            {
                  Toast.makeText(game2.this, "view5", Toast.LENGTH_SHORT).show();
            }

        }
  }

Upvotes: 0

Views: 309

Answers (4)

user370305
user370305

Reputation: 109257

Can this may be problem for (int c=0; c<2; c++)? use for (int c=0; c<3; c++) .. c<3 for 3 columns.. And let me know what happen..

EDIT:

Also

ImageView[] plus=new ImageView[9];

int unique=0;
for (f=0; f<3; f++) {
row[f] = new TableRow(this);
for (int c=0; c<3; c++) {
plus[unique]=new ImageView(this);
plus[unique].setBackgroundResource(randomNumbers[unique]);
plus[unique].setOnClickListener(this);
plus[unique].setId(unique);
row[f].addView(plus[unique], 200,100);
unique++;
}

And in onClick()

 public void onClick(View view) {
           switch(view.getId()){
              case 0:
                   {
                    Toast.makeText(game2.this, "view", Toast.LENGTH_SHORT).show();
                   }
                   .
                   .
                   .
              case 8:
                   {
                    Toast.makeText(game2.this, "view8", Toast.LENGTH_SHORT).show();
                   }
                }
   }

Upvotes: 1

Jayabal
Jayabal

Reputation: 3619

simple set ID to every imageView when creating ImageView. then setOnClickListener().

 onClick(View v) {
    switch(v.getId()) {
      case 1st_ImageViewID:
      break;

      case 2nd_ImageView_ID:
      break;

      case 3rd_ImageView_ID:
      break;

   }
}

Upvotes: 0

Yugandhar Babu
Yugandhar Babu

Reputation: 10349

Use plus[unique] in place of plus[f] in all 3 lines of below loop

for (int c=0; c<2; c++) {
... }

Upvotes: 0

ngesh
ngesh

Reputation: 13501

Use ArrayList instead of array.. Right now you are not adding all the elements.. In nested for loop your elemnts are getting replaced... So use ArrayList... And I don't see a need for such a complex code, You keep UI in XML and still can achieve the Same.. The present code is hard to understand and even you cannot after some days.. You cannot maintain this code..

Upvotes: 1

Related Questions