In a for loop I am placing buttons on the screen and setting their id as
LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout_tags);
layout.setOrientation(LinearLayout.VERTICAL);
Button btn = null;
for (int i = 0; i < buttons_in_row; i++) {
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
for (int j = 0; j < buttons_in_row; j++) {
btn = new Button(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
params.setMargins(5, 5, 5, 5);
btn.setLayoutParams(params);
btn.setText("B " + (j + 1 + (i * buttons_in_row)));
btn.setId(j + 1 + (i * buttons_in_row));
btn.setWidth(width / buttons_in_row);
btn.setHeight(width / buttons_in_row);
btn.setBackgroundResource(R.drawable.button);
GradientDrawable drawable = (GradientDrawable) btn.getBackground();
drawable.setColor(Color.parseColor("#" + colors[random_color][0]));
btn.setOnClickListener(this);
//Log.i("btn.getsize", btn.getWidth() + ", " + btn.getHeight());
row.addView(btn);
}
layout.addView(row);
}
Button b = (Button) layout.findViewById(2);
At the end of the loop I want to pick one of the buttons (e.g. No.2) and give it a different background color.
I tried these
Button b = (Button) findViewById(2);
btn.getId(2);
How can I do that?