Search code examples
androidperformanceandroid-viewfindviewbyid

findViewById vs getChildAt - which one is quicker?


Acquiring a view in the hierarchy I have both of these functions available. While findViewById is easier to use, I often saw suggestions that it can be an expensive operation and should be avoided if possible.

Therefore, would it be any quicker to use getChildAt instead? Is there any guideline as to when it is better to use which?


Solution

  • findViewById vs getChildAt - which one is quicker?

    getChildAt is indeed faster than findViewById, since it is just accessing an array of Views at the specified index.

    Therefore, would it be any quicker to use getChildAt instead

    it will indeed, but you have to know the index of the View you are looking for. If you don't have any mechanism to keep track of the index, than you have to look for it, and you got back to findViewById. You will probably ended up re writing your own version of findViewById. My two cents, don't worry about that.