1

I know how to assing ID's dynamically by invoking setID(). For the ID's to be unique, I used to utilize ids.xml and pass to setID() ID's from the pre-generated pool of ID's.

Question 1: Is there any way to assign ID's without utilizing the ids.xml since I cannot anticipate how many ID's I will need in runtime?

I tried to bypass the first issue presented in Question 1 by dynamically assigning each of which an id based on its label's hash (each label is unique), but there is no way to gaurantee that ID's won't be colliding with ID's auto generated in R.java.

Question 1.1: How the ID naming collision can be resolved?

Question 2: Assume I have the ID value of which I assign and generate dynamically. Since the aformentioned ID does not appear in R.id, findViewById() won't be applicable for retrieving the view. Hence, how can the view be retrieved when the ID is known?

Answer 2: You'd be able to retrieve the view by its corresponding ID only after onCreate() has returned control (terminated).

Mr.
  • 9,429
  • 13
  • 58
  • 82

2 Answers2

8

From API level 17 you can get a new id by calling

View.generateViewId()

Details here.

Community
  • 1
  • 1
X.Y.
  • 13,726
  • 10
  • 50
  • 63
1

Is there any way to assign ID's without utilizing the ids.xml since I cannot anticipate how many ID's I will need in runtime?

This guarantees that every view has a unique ID

for(int i =0 ; i < yourIDcount ; i++){
yourView.setId(i);
}

how can the view be retrieved when the ID is known?

View.findViewById(yourView.getId());

can be used to get your view's id, since every view has a unique Id you can get back the view you wanted..

The word dynamic means which is created at runtime, since you assign id in onCreate it is assigned as the views id, since onCreate is called only once an activity is created, you can make sure that the id you assigned stays intact...

Arif Nadeem
  • 8,524
  • 7
  • 47
  • 78
  • 1
    Regarding the `for` loop, you cannot be sure that you provide a unique ID! Maybe the i-th value is already in use. Reagarding `getId()`, I explicitly written that I have the ID and need not to retrieve the ID, rather, I wish to retrieve the corresponding view. Anyway, thanks for your help. – Mr. Apr 21 '12 at 17:38
  • you seem to misunderstand, you can use findViewById(yourView.getId()) to get back an instance of the view – Arif Nadeem Apr 21 '12 at 17:44
  • No you can't! here is a quote from the Android documentation _Finds a view **that was identified by the id attribute from the XML** that was processed in onCreate(Bundle)._ Note that this ID was not present when the application created. – Mr. Apr 21 '12 at 17:56
  • refer to this please, http://stackoverflow.com/questions/1714297/android-view-setidint-id-programmatically-how-to-avoid-id-conflicts – Arif Nadeem Apr 21 '12 at 18:17
  • 1
    the word dynamic means which is created at runtime, since you assign id in onCreate it is assigned as the views id, however please note that the id will be valid till the app is alive... – Arif Nadeem Apr 21 '12 at 18:24
  • Now I get it, `onCreate()` must return control and only then the ID will be assigned. Thanks. – Mr. Apr 21 '12 at 18:28
  • please edit your original post so I could accept your answer. – Mr. Apr 21 '12 at 18:35