0

i found this helpful post for adding buttons dynamically to a layout, however i am can't understand how to reference those buttons by id (or some other way) to use them in the program. can anyone help me?

Community
  • 1
  • 1
clayton33
  • 4,176
  • 10
  • 45
  • 64

2 Answers2

2

In code why don't you just declare a class level variable? Another common technique is to save references as tags or save whole bunch of references in the holder object and save that as a tag

Bostone
  • 36,858
  • 39
  • 167
  • 227
  • "In code why don't you just declare a class level variable? Another common technique is to save references as tags or save whole bunch of references in the holder object and save that as a tag" wow that kind of went over my head, i tried to google the things you suggested, but my level of understanding is rather basic... could you perhaps provide an example of what you mean by "class level variable", or "save reference as tag"? – clayton33 Nov 28 '10 at 03:48
  • Class level variable is also called a filed, something that is declared on class level. If you don't understand these concepts it probably will be impossible for you to program Java with any sort of efficiency. I would suggest getting familiar with general programming concepts – Bostone Nov 29 '10 at 05:23
1

I had the same situation.Just use Tag, and assign them an id that you can use in a loop. See example below for some images and assignment of tags and touch listener, but you can you it for buttons or anything you want. Now you can use loops to change things about each button:

for (int i = 0; i < 8; i++)
        {
        String bid = "WLButton"+i;
        int resID = getResources().getIdentifier(bid, "id", "com.head");
        wlbutt[i] = (ImageView) findViewById(resID);
        wlbutt[i].setTag(i);
        wlbutt[i].setOnTouchListener((OnTouchListener) WLListener);   
        }
Vivek Kalkur
  • 2,200
  • 2
  • 21
  • 40
neal
  • 11
  • 1