Reputation: 167
I have a tableLayout in Android and each row in the table has an ImageButton, a TextView and another ImageButton. The no. of rows is variable and I am generating the table programmatically at runtime.
I am setting id of the buttons to be same as row number by using ImageButton.setId(i)
.
When the user clicks on any ImageButton, I want to be able to access its id in the onClickListener. Is there a way to do it? All answers on stackoverflow which I have seen regarding this involve a fixed no. of buttons and use a switch statement to find out the id. But since the no. of buttons is dynamic for me, this won't work for me.
Here's the code:
TableLayout tbl_layout = (TableLayout) findViewById(R.id.tbl_layout);
for (int i=0; i<num_rows; i++) {
TableRow tbl_row= new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
tbl_row.setLayoutParams(lp);
tbl_row.setPadding(0, 5, 0, 5);
//tbl_row.setBackground(R.drawable.border);
if(i == 3)
tbl_row.setBackgroundColor(Color.GREEN);
else
tbl_row.setBackgroundColor(Color.parseColor("#ffffd1"));
tbl_row.setGravity(Gravity.CENTER);
TextView tv = new TextView(this);
tv.setGravity(Gravity.CENTER);
tv.setText("Some text goes here\New line.");
ImageButton infoBtn = new ImageButton(this);
infoBtn.setImageResource(R.drawable.ic_menu_info);
infoBtn.setAdjustViewBounds(true);
infoBtn.setScaleType(ImageView.ScaleType.FIT_XY);
infoBtn.setMaxHeight(120);
infoBtn.setMaxWidth(120);
infoBtn.setId(i);
//infoBtn.setBackgroundColor(Color.BLUE);
ImageButton cameraBtn = new ImageButton(this);
cameraBtn.setImageResource(R.drawable.ic_menu_camera);
cameraBtn.setAdjustViewBounds(true);
cameraBtn.setScaleType(ImageView.ScaleType.FIT_XY);
cameraBtn.setMaxHeight(120);
cameraBtn.setMaxWidth(120);
cameraBtn.setId(i);
tbl_row.addView(infoBtn);
tbl_row.addView(tv);
tbl_row.addView(cameraBtn);
tbl_layout.addView(tbl_row, i);
}`
Upvotes: 0
Views: 122
Reputation: 5542
for (int i=0; i<num_rows; i++) {
TableRow tbl_row= new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
tbl_row.setLayoutParams(lp);
tbl_row.setPadding(0, 5, 0, 5);
//tbl_row.setBackground(R.drawable.border);
if(i == 3)
tbl_row.setBackgroundColor(Color.GREEN);
else
tbl_row.setBackgroundColor(Color.parseColor("#ffffd1"));
tbl_row.setGravity(Gravity.CENTER);
TextView tv = new TextView(this);
tv.setGravity(Gravity.CENTER);
tv.setText("Some text goes here\New line.");
ImageButton infoBtn = new ImageButton(this);
infoBtn.setImageResource(R.drawable.ic_menu_info);
infoBtn.setAdjustViewBounds(true);
infoBtn.setScaleType(ImageView.ScaleType.FIT_XY);
infoBtn.setMaxHeight(120);
infoBtn.setMaxWidth(120);
//infoBtn.setId(i);
//infoBtn.setBackgroundColor(Color.BLUE);
infoBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
ImageButton cameraBtn = new ImageButton(this);
cameraBtn.setImageResource(R.drawable.ic_menu_camera);
cameraBtn.setAdjustViewBounds(true);
cameraBtn.setScaleType(ImageView.ScaleType.FIT_XY);
cameraBtn.setMaxHeight(120);
cameraBtn.setMaxWidth(120);
//cameraBtn.setId(i);
cameraBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
tbl_row.addView(infoBtn);
tbl_row.addView(tv);
tbl_row.addView(cameraBtn);
tbl_layout.addView(tbl_row, i);
}`
Upvotes: 1