Sobes.tech
Junior — Middle+

Creating your own findViewById equivalent for View

livecode

Task condition

It is necessary to implement a function that searches for a nested View by its identifier, mimicking the behavior of the standard findViewById method. When writing the solution, you can use the following properties and functions provided in the SDK:

  • View.id: Int
  • ViewGroup.childCount: Int
  • fun ViewGroup.getChildAt(index: Int): View
  • fun ViewGroup.children(): Sequence<View>

You need to write a universal function that returns the found element of the required type or null if the element with the specified id is not found.

fun <T: View> findViewById(root: View, id: Int): T?