0

How do I create multiple TextViews with one ID in one layout? This is my XML:

<TextView
                android:id="@+id/tooltext"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dp"
                android:textColor="#ffffff"
                android:textStyle="bold"
                android:textSize="20sp"
                android:layout_gravity="center"/>

<TextView
                android:id="@+id/tooltext"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dp"
                android:textColor="#ffffff"
                android:textStyle="bold"
                android:textSize="20sp"
                android:layout_gravity="center"/>
<TextView
                android:id="@+id/tooltext"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dp"
                android:textColor="#ffffff"
                android:textStyle="bold"
                android:textSize="20sp"
                android:layout_gravity="center"/>

and this my Java code

TextView lVl = (TextView) findViewById(R.id.tooltext);
lVl.setText("b a g i n n e r");

When I run, only one TextView appears. Why?

MGA-AL
  • 7
  • 3
  • 1
    It's not really possible like that. What's the reason to not just give them different ids? – jle Aug 22 '18 at 12:40

2 Answers2

1

You can't

IDs are layout-wide unique.

You could not name them but put in one layout.

After that circulate over them inside that layout and set the same value to all of them. Like here: Get all child views inside LinearLayout at once

Taier
  • 2,109
  • 12
  • 22
  • Are you sure? Why don't you try it? –  Aug 22 '18 at 12:54
  • @mTak practically you can name 2 views the same in 1 layout and it would compile. But what the point of it? findViewByID return only 1 view. That would make unpredictable code, and would not help the author in any significant way. – Taier Aug 22 '18 at 13:28
  • I already mentioned it is not a good practice, it is not recommended but **it is possible** –  Aug 22 '18 at 13:31
0

It's not really possible like that. You can use ListView