For example, 1 lavout may contain a different (previously unknown) amount of textview. Is there a way to extract text from them and put it in a variable?

  • go through all the children and check for an instanceof TextView ... but at some point it’s probably not particularly correct) - ermak0ff
  • @ ermak0ff, and why is it wrong? .. - Yuriy SPb
  • @ YuriySPb well, this is most likely because of the well-known tale "about the instanceof speed and if there is a need for it, then the application architecture is most likely wrong" .... but this is just "tale")) - ermak0ff
  • @ ermak0ff, well, then you can try wrap it in a try ))) - YuriiSPb
  • This is partly true, because even if we generate a TextView dynamically, it is easier to store links to them, and not to sort through the entire root layout - ermak0ff

1 answer 1

  1. Cycle through all the children in the container.
  2. Check if the child is TextView
  3. If yes, remove the text.

     String text = ""; for(int i = 0; i<someContainer.getChildCount(); i++) { if(someContainer.getChildAt(i) instanceof TextView) { text += ((TextView)someContainer.getChildAt(i)).getText().toString(); } } 
  • I tried the variant you proposed, + passed text into textview2, it turns out that the contents of only the last textview from which the text was extracted are transferred to textview2. Therefore, the text variable stores only 1 text. - Elena Chchchchchchch
  • @ ElenaChchchchchchch, that's right. I wrote exactly what you asked for - for each text field in the container, an object of the string type is created and the text from the text field is placed into it. To get the text of all the fields in one variable, you do not need to create a new variable each time, but to supplement the existing one. See updated code. - Yuriy SPb