6

I am making an app in which I want to change the text of textviews from an array of strings. For that I need to make the array of textviews.How to do that?? Can anyone help me over this

4 Answers 4

27

You can create TextViews like this:

int textViewCount = 10;

TextView[] textViewArray = new TextView[textViewCount];

for(int i = 0; i < textViewCount; i++) {
   textViewArray[i] = new TextView(this);
}
Sign up to request clarification or add additional context in comments.

Comments

3

May be its useful for you i use button array so i am gussing textview work like that:

TextView[ ][ ]  _txt;   

_txt = new TextView[_dimension][_dimension]; // _dimension = 5 what you want
_txt[0][0] = (TextView) findViewById(R.id.text1);
_txt[0][1] = (TextView) findViewById(R.id.text2);

and more...

1 Comment

That was my requirement so you can use single array instead of 2d
3

If you want large number of textviews, in that case to avoid OutofBound exception use following code

LinearLayout parent = new LinearLayout(this);
        TextView textView;
        for( i = 0; i < count; i++) {
            textView = new TextView(this);
            textView.setTag(""+i);// setting tag with index i
            parent.addView(textView);
        }
        int len=parent.getChildCount();
        int j = 0;
        int requiredPosition = 5;
        while(j<len) {
            TextView tempTextView =((TextView)parent.getChildAt(i)); 
            if( tempTextView.getTag().equals(""+requiredPosition)){
                //Perform required operation
                //tempTextView.setText("");
            }
            j++;
        }

Comments

1

You can do achieve that with something like this:

int textvwCount = 20;//number of textview you want to use in an array

TextView[] arrTxtView = new TextView[textvwCount ]; // declare and assign array length of text views.

for(int i = 0; i < textViewCount; i++) { // iterate over all array items and assign them text.
  Textview txtCnt = new TextView(this);
txtCnt .settext(i);
 textViewArray[i] =txtCnt ;
}

Comments

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.