1

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

1 Answer 1

2

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]));
       ...
  }
Sign up to request clarification or add additional context in comments.

1 Comment

Oh right yes, I see, though I had to use textview.setId(item) as parseInt wasn't working. But thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.