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" />
You could have a labels collection as well to fetch labels form.
It will read
id
at run time. May be you have100 ids
then there is no need to save these id's in an array.Note
And use getActivity() with
getResources()
andgetPackageName()
inside Fragment. For details have a look at this and thisPerformance wise good approach.
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: