Search code examples
androidxamarintextview

Dynamically adding data to more TextView


Is it possible to dynamically add text to several textview that are defined in xalm? For example, in a loop:

var textView1 = FindViewById<TextView>(Resource.Id.textView1);
var textView2 = FindViewById<TextView>(Resource.Id.textView2);
var textView3 = FindViewById<TextView>(Resource.Id.textView3);

var content = "Add me to more at one textView"

for (i = 1; i = 3; i++)
{
// add me;
}

I can easily do it as it adds textview programmatically by calling GetChildAt () in layout. Is it possible to add dynamic if textView is defined in xalm?


Solution

  • Say your texts ids are text1, text2, tex3, etc. Use:

    for (int i=1; i<numberOfTextsViews; i++) {
       int id = this.getResources().getIdentifier("text" + i, "id", getPackageName());
       TextView temp = findViewById(id);
       temp.setText("text you want to add to all your texts");
    }