Search code examples
javaandroidtextviewandroid-edittext

How can I select multiple TextViews with different IDs in Java Android Studio like findViewById(R.id.)


I am doing a project and this partly has a function of a search engine that highlights the words that are searched.

I have an EditText and a button that when pressing the button highlights the word in the EditText. It works perfectly if you search for it in a single TextView as I assign the findViewById(R.id.textView1) to it but how do I do if I have more than 100 TextView? I'm doing a long manual. In addition, each TextField in the XML has different names. For example textView1, txView1, textView2, txVi1, etc

Is it possible what I need to do? I looked in many places and couldn't find anything.

I leave you part of the .java code

public class Medicina extends AppCompatActivity {

EditText searchview;
Button btnBuscar;


boolean isHighlight = false;
TextHighlighter textHighlighter;

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_medicina);



    searchview = (EditText)findViewById(R.id.searchview);
    btnBuscar = (Button)findViewById(R.id.btnBuscar);

    btnBuscar.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            if (!isHighlight){
                textHighlighter = new TextHighlighter();

                textHighlighter.setBackgroundColor(Color.parseColor("#FFFF00"))
                        .setBackgroundColor(Color.RED)
                        .addTarget()
                        .highlight(searchview.getText().toString(),TextHighlighter.BASE_MATCHER);
            }


            isHighlight = !isHighlight;
        }

    });


}

In the .addTarget() would go the findViewById (R.id.lalala) And it would work if you only had a single TextView, but it's hundreds.

Thank you

The first almost 300 lines of XML code (from here on it's all the same, the vast majority TextView and ImageView). I upload the code to GitHub because it gives me an error when pasting it in the post.

https://github.com/lautarovculic/activity_medicina


Solution

  • You can assign an id for the ViewGroup that contains all the TextView. Then obtain the ViewGroup by findViewById(R.id.viewgroup) and loop through all ViewGroup's child view to get all of its TextView.

    public List<TextView> getAllTextViewsSimple() {
        final List<TextView> result = new ArrayList<>();
        final ViewGroup viewGroup = findViewById(R.id.viewgroup);
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            final View v = viewGroup.getChildAt(i);
            if (v instanceof TextView) {
                 result.add((TextView) v);
            }
        }
        return result;
    }
    

    If you want an even more general approach in case the TextView may not be placed in the same ViewGroup. Assign an id to the outmost ViewGroup then obtain and pass it as a parameter to a recursive function to get all child TextView.

    public List<TextView> getAllTextViews(View v) {
        final List<TextView> result = new ArrayList<>();
        if (v instanceof ViewGroup) {
            final ViewGroup viewGroup = (ViewGroup) v;
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                result.addAll(getAllTextViews(viewGroup.getChildAt(i)));
            }
        } else if (v instanceof TextView) {
            result.add((TextView) v);
        }
        return result;
    }
    

    The code snippet to help you use it with TextHighlighter

    final List<TextView> allTextViews = getAllTextViewsSimple();
    textHighlighter.setBackgroundColor(Color.parseColor("#FFFF00"))
                            .setBackgroundColor(Color.RED);
    
    for (TextView tv : allTextViews) {
        textHighlighter.addTarget(tv);
    }                    
    
    textHighlighter.highlight(searchview.getText().toString(),TextHighlighter.BASE_MATCHER);