4

I have to dynamically create 4 Buttons and I have to generate dynamically their id, something like:

  for(int i = 1; i <= 4; i++){
    Button button = new Button(activity);

    // i need something here to turn this string into an int;    
    button.setId("button" + i) 
  }

I know how to dynamically get the id of an existing view:

getResources().getIdentifier("button2", "button", getPackageName())

What I don't know, is how to generate the id dynamically.

I looked on the internet but I didn't find in anything.

I need something like:

int id = getResources().setIdentifier("button2", "button", getPackageName());
button.setid(id);

Thank you in advance.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
MDP
  • 4,177
  • 21
  • 63
  • 119
  • `setId(int)` pass integer value as argument – M D Jan 17 '17 at 09:50
  • 1
    why do you want to call `setId()` at all? you have the references to those `Button`s, so what do you need those IDs for? – pskink Jan 17 '17 at 09:50
  • 2
    Just play with `.setTag(string value)`. it's more useful – M D Jan 17 '17 at 09:51
  • 1
    because, later, I need to identify by id the button I created and manipulate them – MDP Jan 17 '17 at 09:51
  • again, you have references to those views, no need for any ID – pskink Jan 17 '17 at 09:52
  • 1
    Hi @pskink, I edited my code. I don't understand how to identify the references, since the buttons are dinamically generated. There is something that I don't understand in what your are saying to me – MDP Jan 17 '17 at 10:00
  • 1
    As it was already mentioned, you have reference to button, just use .setTag(Object) to identify buttons, no need to have any ID. – Thror Jan 17 '17 at 10:00
  • Ok, I'll use setTag. Thanks to everybody :) – MDP Jan 17 '17 at 10:03
  • Related post - [How to set Id of dynamic created layout?](https://stackoverflow.com/q/8937380/465053) – RBT Dec 23 '19 at 10:48

4 Answers4

9

A better practice would be to use button.setId(View.generateViewId()) which was introduced in API 17.

Montassir Ld
  • 531
  • 4
  • 12
4

You can just use the View.setId(int) for this. According to documentation id need not to be unique in a tree heirarchy, you can use any (positive) Integer for the Views you add programmatically.

Doc says

The identifier does not have to be unique in this view's hierarchy. The identifier should be a positive number.

if you want some other info attached with the View you can tag objects to view using setTag(), and look by findViewWithTag("obj");

see more about creating ids here https://stackoverflow.com/a/13241629/5235032

Community
  • 1
  • 1
Jayanth
  • 5,954
  • 3
  • 21
  • 38
3

1、create a xml in values folder,

2、<resources> <item type="id" name="button1">123</item> </resources>

3、use it in java code button.setId(R.id.button1);

志威梦
  • 136
  • 3
0

Had the same issue, only for ImageView.
Since I've found no answer, and everyone basically said either
"not possible" or "use xml", I've created my own hashMap. https://stackoverflow.com/a/53321713/10295425

RazVan
  • 36
  • 7