Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Button ID programmatically

Tags:

android

button

Android 2.3.3

I have a table with N rows and N columns. For each row, I should add 4 buttons dynamically, and later do actions based on the button clicked. I know we can set the button IDs with Integer values with button.setID(), but I want to know whether we can set IDs as string values as we set in XML files, such as btnXYZ1 and btnXYZ2 etc.,

like image 488
Vamsi Challa Avatar asked Feb 11 '13 08:02

Vamsi Challa


People also ask

How to set a button in android studio?

To define the click event handler for a button, add the android:onClick attribute to the <Button> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.

How to change button background in android?

To set Android Button background color, we can assign android:backgroundTint XML attribute for Button in layout file with the required Color Value. To programmatically set or change Android Button background color, we may call pass the method Button.


1 Answers

You can use tags for that purpose . For example

btn.setTag("btXYZ");
like image 86
pvn Avatar answered Sep 22 '22 17:09

pvn