findViewById - Internals
findViewById in Android

findViewById - Internals

Hope everyone is doing well.

Today I will be writing about the internal working of findViewById in Android.

I am sure, we all have used findViewById extensively in our Android projects to get access to a certain view in our layout hierarchy.

It all starts from this -

findViewById example

Tracking the flow further it goes to this in AppCompatActivity -

AppCompatActivity

Further it goes to this point in AppCompatDelegateImpl-

AppCompatActivityDelegateImpl

Over here it performs an important action before delegating the task further, it performs something called ensureSubDecor(). This method basically ensures that the basic views for the screen have been rendered before accessing a view on the screen. This might come to our rescue when we lazily inflate our layout and using findViewById to access its views for the first time.

Moving further, the Window's findViewById() is called -

Window

And then finally the View's findViewById() is called -

View

Over here, something interesting happens. It checks if the id of the current view matches the one we are searching for. In case it matches it returns the view else it does a traversal as mentioned in the above code named findViewTraversal(). Check below for its implementation-

findViewTraversal

Now the issue is it just checks if the current id matches the searched id, what if the searched view is the one among its children ?

So the final implementation for it is mentioned in the findViewTraversal() inside the ViewGroup class -

ViewGroup

It uses a popular Graph traversal technique called DFS(Depth first search), where it checks itself first. If it matches it returns the view else it iterates over its children and checks the same recursively.

Hope you learnt something new today, do follow me for more such content.

Thanks

Hey Siddhant, as a fellow Android dev, it's always good to keep options open. Check out Mirajobs for exploring new opportunities without any risk to your current role. It's a great way to stay ahead, especially with the tech market being so unpredictable lately.

Aman Rawat

Android Developer | 3+ Years' Experience | Proficient in Jetpack Compose, App Security & Performance

1 个月

Interesting

要查看或添加评论,请登录

Siddhant Mishra的更多文章

  • SuspendCoroutine

    SuspendCoroutine

    Today I will be writing about the usage of an important concept in Android namely suspendCoroutine. By definition…

  • Lambda functions

    Lambda functions

    Today I will be writing about two different yet intriguing way to write the lambda functions in Kotlin. Conventionally…

    1 条评论
  • Exceptions in Coroutines

    Exceptions in Coroutines

    Hi everyone, today I will be writing about exceptions in Coroutine and how to handle them gracefully. First let us see…

  • Trie data structure

    Trie data structure

    Today I will write about a magical data structure namely Trie. When we talk of storing and retriving some information…

  • Vertical Order traversal in binary tree

    Vertical Order traversal in binary tree

    We generally talk about Inorder, PreOrder, PostOrder and LevelOrder traversals in binary tree, but we generally do not…

  • LCA in Binary Tree

    LCA in Binary Tree

    Today I will be writing about the LCA(Least Common Ancestor) for a binary tree. LCA refers to the node in the binary…

  • Delegates in Kotlin

    Delegates in Kotlin

    Today I will be writing about my understanding regarding the delegates in Kotlin. The english meaning of Delegation…

  • TypeAlias in Kotlin

    TypeAlias in Kotlin

    Today I will be writing about TypeAlias in Kotlin and how do I understand and use it in my day to day coding. Typealias…

  • Reified in Kotlin

    Reified in Kotlin

    Today I will be writing about the type erasure in Java and Kotlin and its solution. The concept of type erasure stems…

    1 条评论
  • Jetpack Compose - Interesting features to dive deeper into.

    Jetpack Compose - Interesting features to dive deeper into.

    Recently I got to migrate my sample project from the conventional XML to Jetpack Compose, and I would like to list down…

    3 条评论