I'm trying to generate random ids for views as shown in following screenshot. But it didn't work. It got null. How should I findViewById ?
Asked
Active
Viewed 1.9k times
5
-
You're confused. `setID` and `findViewByID` both are different. `findViewByID` allows you to refer to the particular view with the id you give in the xml file. – Aniruddha Aug 05 '14 at 11:53
-
Why are you trying to do this. This is a wrong thing to do. I mean when you create a view you give it an id then how can you expect to access that view by generating some random id. – Sar009 Aug 05 '14 at 11:56
-
So.., what am I supposed to do to get back textView ? – Zin Win Htet Aug 05 '14 at 11:56
-
@Sar009 , cuz I've to create a lot views dynamically and got to reuse them from somewhere in the project. – Zin Win Htet Aug 05 '14 at 11:58
-
2why not try ListView when you have to create a LOT OF views dynamically. – Sar009 Aug 05 '14 at 12:02
-
I can't use ListView. Cuz my views depends on JSON from server. I don't predict what will be coming. – Zin Win Htet Aug 05 '14 at 12:03
6 Answers
2
TextView tv = new TextView(this);
This means you're creating the TextView dynamically. So you don't need to do findViewById
.
findViewById
is used when the view with id is present in xml file.
Remove the TextView cloneTextView = (TextView) findViewById(randomNo)
line. Your question is vague, I tried to explain.

Aniruddha
- 4,477
- 2
- 21
- 39
-
Ok. Let's say I made TextView tv = new TextView(this). How can I call it from another activity without knowing id? Don't say to make static. My views will be create dynamically so there's no static. – Zin Win Htet Aug 05 '14 at 12:00
-
You want the value of that `TextView`? If so, you can pass the value using Intent. – Aniruddha Aug 05 '14 at 12:02
2
Best practices for unique identifiers
Java
String uniqueID = UUID.randomUUID().toString();
Kotlin
var uniqueID = UUID.randomUUID().toString()

Abdul Basit Rishi
- 2,268
- 24
- 30
0
I got my own solution... It should be like that..
Random r = new Random();
randomNo = r.nextInt(1000+1);
TextView textView = new TextView(this);
textView.setId(randomNo);
linearLayout.addView(textView);
int childCount = linearLayout.getChildCount();
for(int i=0;i<childCount;i++){
if(linearLayout.getChildAt(i).getId()==randomNo){
TextView cloneTextView = (TextView) linearLayout.getChildAt(i);
cloneTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
cloneTextView.setText("I'm a clone...!");
linearLayout.removeAllViews();
linearLayout.addView(cloneTextView);
}
}
It works and that's what I want. Thank you all.

Zin Win Htet
- 2,448
- 4
- 32
- 54
0
Something like this may work.
But I'm not sure about possible performance and memory issues, since it will return an instance of a view (if found). During a little test sequence it never hit an existing id, with other words the first random number was always ok.
private int createUniqueId() {
int id = RandomUtils.nextInt();
while(findViewById(id) != null) {
//id is not unique, try another one...
id = RandomUtils.nextInt();
}
//return unique id
return id;
}

Dirk
- 2,011
- 1
- 20
- 25
0
You can create UUID (universal unique identifier) as follow :
String id= UUID.randomUUID().toString();

Osama Ibrahim
- 995
- 10
- 13
-
1While code-only answer are allowed on Stack Overflow, it's always better to include a brief explanation, so that other users will understand it faster and better :) – Federico Grandi Feb 24 '19 at 10:35
-