Search code examples
javaandroidarraystextview

Is there any way to initialize an array of text views in java android studio


I have seven text views (textview0....textview6) in my xml file and in my java file I want to loop through these views but I faced a problem that I can't use the findViewById function because I can't use the index (i) in the name I want something similar to this

for(int i = 0 ; i<=6 ;i++){
textview[i] = findViewById(R.id.textview+i)}

Solution

  • Try this

    for(int i = 0 ; i<=6 ;i++) {
        int resourceId = this.getResources().getIdentifier("textview"+ i, "id", this.getPackageName());
        textview[i] = findViewById(resourceId);
    }