Brendan
Brendan

Reputation: 67

Create a TextView with an ID from a String Array - Android

In Android, I am wanting to create several TextViews, programmatically, with their own individual IDs, so that I can use, for example a FOR statement to create a certain amount of TextViews, with their own IDs.

For example,

// Created TextView prior

for(int i=0;i<10;i++) {
    linearlayout.addView(ID);
}

So I was wondering if it would be possible for me to have a String Array with a few different IDs and somehow give each TextView the its ID by the position of the String Array. So, for example, when the String Array is 'frogs' the ID of the TextView is 'frogs' and then as the FOR statement increases, so the value of the String Array defining the TextView ID progresses too (If it is even possible at all).

Thanks, Brendan

Upvotes: 1

Views: 2753

Answers (1)

Ahmad Kayyali
Ahmad Kayyali

Reputation: 8243

so I was wondering if it would be possible for me to have a String Array with a few different IDs and somehow give each TextView the its ID by the position of the String Array

Yes you can have that:

String[] ids = new String[] {"1","2","3","4","5"};
for(int item = 0; item < ids.length; item++) 
 {
       TextView textView = new TextView(this);
       // careful id value should be a positive number.
       textView.setText(ids[item]);
       textView.setId(parseInt(ids[item]));
       ...
  }

Upvotes: 2

Related Questions