Tymek T.
Tymek T.

Reputation: 145

TextViews text change in a for loop

I want to change textViews' texts inside for loop in Android Studio, something like that:

for i=0 ------------> textView0 set text "Default"

for i=1 ------------> textView1 set text "Default"

for i=2 ------------> textView2 set text "Default"

Is it possible to do that? I don't know how to change the textView id inside the loop according to "i" value, can someone help me?

Here's my XML:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="--------------"
    android:id="@+id/textView0"
    android:layout_marginLeft="16dp"
    android:layout_gravity="center_horizontal"
    android:textSize="13dp" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="--------------"
    android:id="@+id/textView1"
    android:layout_gravity="center_horizontal"
    android:layout_marginLeft="16dp"
    android:textSize="13dp" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="--------------"
    android:id="@+id/textView2"
    android:layout_gravity="center_horizontal"
    android:layout_marginLeft="16dp"
    android:textSize="13dp" />

Upvotes: 2

Views: 5497

Answers (4)

Zar E Ahmer
Zar E Ahmer

Reputation: 34360

There is another better approach to read layout, drawable and id resource ID in android

    String[] messages = {"one", "two", "three"};

    for (int i = 0; i < 3; i++) {
        TextView tv = (TextView) findViewById(getResources().getIdentifier("textView" + i+1, "id", getPackageName()));
        tv.setText(messages[i]);
    }

It will read id at run time. May be you have 100 ids then there is no need to save these id's in an array.

Note

And use getActivity() with getResources() and getPackageName() inside Fragment. For details have a look at this and this

Upvotes: 0

Akeshwar Jha
Akeshwar Jha

Reputation: 4576

Well, if you have all the textviews at the same hierarchy level as you currently have in your xml, you can try this solution:

Wrap the whole thing up in one parent layout, say a RelativeLayout. Then, loop through it:

int numberOfTextViews = layout.getChildCount();
for(int i = 0; i < numberOfTextViews; i++) {
    ((TextView)(afterSendingMail.getChildAt(i))).setText("Default");
} 

Upvotes: 0

Sohail Zahid
Sohail Zahid

Reputation: 8149

Performance wise good approach.

    int[] textViews = {R.id.textView1, R.id.textView2, R.id.textView3};
    String[] messages = {"one", "two", "three"};

     for (int i = 0; i < 3; i++) {
            TextView tv = (TextView) findViewById(textViews[i]);
            tv.setText(messages[i]);
        }

Upvotes: 5

malmling
malmling

Reputation: 2518

You could have a labels collection as well to fetch labels form.

List<String> labels = new ArrayList<>();
labels.add("Default 1");
labels.add("Default 2");
labels.add("Default 3");

int[] textViewIds = new int[3];
textViewIds[0] = R.id.text_view_a;
textViewIds[1] = R.id.text_view_b;
textViewIds[2] = R.id.text_view_c;

for (int i = 0; i < textViewIds.length; i++) {
    TextView tv = (TextView) findViewById(textViewIds[i]);
    tv.setText(labels.get(i));
}

Upvotes: 0

Related Questions