45

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.

Russell Stewart
  • 1,960
  • 4
  • 24
  • 31
  • possible duplicate of [Android : How to get root view from current activity?](http://stackoverflow.com/questions/4486034/android-how-to-get-root-view-from-current-activity) – Matthew Mar 30 '11 at 15:42
  • @rds You linked it with wrong question imo – Ajay S Aug 21 '14 at 19:03
  • @TGMCians Oops, the technique can be the same, but the questions are indeed different. I retracted my close vote. – rds Aug 21 '14 at 22:31
  • 1
    getActivity().getWindow().getDecorView().getRootView(); – Pramod Garg May 17 '17 at 13:00

1 Answers1

111

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.

Community
  • 1
  • 1
Matthew
  • 44,826
  • 10
  • 98
  • 87