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
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
kalpana c
That was my requirement so you can use single array instead of 2d
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
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 ; }