Getting root view of Activity in Android

Android root view

Sometimes we need to get root view of Android Activity. There are different approaches on how to do it.

Approach 1 — specify view id

Just specify view id of the root view in your layout:

<FrameLayout
    android:id="@+id/rootView"
    ...

And then find it in your code:

val rootView = findViewById(R.id.rootView)

Approach 2 — use android.R.id.content

Just get the root view using android R class:

val rootView = findViewById(android.R.id.content).getRootView()

, ,

Leave a Reply

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