Any easy, generic way in Android to get the root View of a layout?
I'm wondering if there is a general-purpose method in the Android Activity class to allow you to get the root View of that Activity's layout. I know I can do it this way:
View v = findViewById(R.id.root_view_id)
But I would like to be able to do it without having to pass in the ID of the root view. I need to do this for a lot of different Activities, and it would be a lot more convenient if I had a one-size-fits-all method call to accomplish the task.
Solution 1:
This answer and comments give one method:
findViewById(android.R.id.content)
Given any view in your hierarchy you can also call:
view.getRootView()
to obtain the root view of that hierarchy.
The "decor view" can also be obtained via getWindow().getDecorView()
. This is the root of the view hierarchy and the point where it attaches to the window, but I'm not sure you want to be messing with it directly.