Find all the views containing a particular keyword in Tag

Updated 26 October 2021

Save

While making dynamic views in your android application, we often feel the need to find all the views containing a particular keyword in Tag, associated with the view itself so that we can perform some action on all of the views later.In this blog, we will learn how to Find all the views containing a particular keyword in Tag.

Implementing this is super easy, all you need to know is familiarity with the lists and recursion. The simple plain logic of adding to the list will get you the desired result.

APPROACH:

  1. While creating dynamic views you need to add a particular Tag keyword that you will later use to search all the views.
  2. Create a function that takes in a root view to traverse and the tag keyword as input and returns an ArrayList of views.
  3. In the function, you just created traverse the whole root view.
  4. While traversing compare the tag of the current child and if the keyword to be searched is contained in the Tag of the child, add this to the list to be returned
  5. If the Current Child is a viewGroup then add all the child views containing the tag by adding to the returning list through recursion.

That’s it conceptually you have obtained the desired result. Let’s have a look at the code.

 

CODE:

Function to get all the views:

Using it in your code :

That’s All.
Keep coding and Keep Sharing

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home