Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: array of textviews

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

like image 576
ekjyot Avatar asked Oct 08 '11 04:10

ekjyot


People also ask

How to create array of TextView in android?

Go to app > res > layout > right-click > New > Layout Resource File and create a new layout file and name this file as item_view. xml and make the root element as a LinearLayout. This will contain a TextView that is used to display the array objects as output.

How to make text size responsive in android?

With Android 8.0 (API level 26) and higher, you can instruct a TextView to let the text size expand or contract automatically to fill its layout based on the TextView 's characteristics and boundaries. This setting makes it easier to optimize the text size on different screens with dynamic content.

What is text view in android?

A TextView displays text to the user and optionally allows them to edit it. A TextView is a complete text editor, however the basic class is configured to not allow editing.

How to change text size of TextView in android studio?

Go to File -> Settings, a new setting dialogue box will appear. Then go to Editor -> General. Now mark the checkbox Change font size with Ctrl + Mouse wheel and click on Apply button. Now to change your editor font size, you just have to press and hold Ctrl and rotate the Mouse wheel.


2 Answers

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);
}
like image 116
evilone Avatar answered Oct 11 '22 16:10

evilone


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++;
        }
like image 45
Sandy Avatar answered Oct 11 '22 16:10

Sandy