I know how to get the root view with View.getRootView(). I am also able to get the view from a button's onClick
event where the argument is a View. But how can I get the view in an activity?
-
3In activity, normally you tell which resource it should render using `setContentView()` and the view that you supplied is already the root. If you need the handle of that view, simply put an ID to it in XAML and `findViewById()` would be fine. – xandy Dec 20 '10 at 01:05
-
My plan is to attach the code dynamically .. so if my users use the api I expect it to be automatically detect things.. Boulder's solution works ! – Lalith Dec 21 '10 at 06:42
-
5@xandy: a slight typo: XAML -> XML. – superjos Jan 18 '12 at 12:53
12 Answers
If you need root view of your activity (so you can add your contents there) use
findViewById(android.R.id.content).getRootView()
Also it was reported that on some devices you have to use
getWindow().getDecorView().findViewById(android.R.id.content)
instead.
Please note that as Booger reported, this may be behind navigation bar (with back button etc.) on some devices (but it seems on most devices it is not).
If you need to get view that you added to your activity using setContentView()
method then as pottedmeat wrote you can use
final ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this
.findViewById(android.R.id.content)).getChildAt(0);
But better just set id to this view in your xml layout and use this id instead.

- 6,077
- 3
- 38
- 44

- 22,222
- 4
- 42
- 56
-
203Actually just findViewById(android.R.id.content) is giving me the root view. If that is not true in some cases I can get root view from the findViewById(android.R.id.content).getRootView(). Thanks for the answer. Where can I learn more about android.R stuff ? I wasn't aware of it. – Lalith Dec 21 '10 at 06:39
-
5You can check here I suppose http://developer.android.com/reference/android/R.html It's just android resources reference. Personally I learned about android.R.id.content then checking layouts in hierarchyviewer. – Dmitry Ryadnenko Dec 21 '10 at 07:29
-
14I've noticed that this view appears to include the status bar, so if you're looking for the visible part of your activity, use the answer from @pottedmeat. – Ben Clayton Mar 17 '12 at 13:16
-
6@Lalith, can you elaborate on when you needed to do findViewById(android.R.id.content).getRootView()? A general rule would be really useful to know. – batbrat Mar 29 '14 at 09:03
-
2@batbrat I need to use .getRootView() in Android 5.0+ when using action bar – Morten Holmgaard Jul 23 '15 at 17:19
-
1When you use this answer, your Snackbar will include the system decor (meaning the Snackbar information will appear behind the Navigation Buttons - Home/Back/Recents). I need my Snackbar to appear in MY Activity (not the entire system, including the on-screen nav buttons), so use the other answer. – Booger Sep 28 '15 at 14:31
-
-
-
@batbrat: IIRC, if you already have the root view, `view.getRootView()` will return that root view (again). I mean, it is harmless to add `.getRootView()`, so the safe answer is to *always* do `findViewById(android.R.id.content).getRootView()`. [Unless you want the root of content, in which case the `.getChildAt(0)` is what you want.] – ToolmakerSteve Jan 19 '17 at 23:50
-
1`activity.findViewById(android.R.id.content)` is returning null in Android 5. :( – Juan José Melero Gómez May 22 '17 at 09:47
-
1@JuanJoséMeleroGómez Is it consistent for all Android 5 devices or is it only for your device? – Dmitry Ryadnenko May 22 '17 at 12:15
-
You're right, @DmitryRyadnenko, it only happens in my device. It's a Samsung SM-T365. I had to go with `getWindow().getDecorView()` on it. – Juan José Melero Gómez May 31 '17 at 09:43
-
Perfect working. Without this approach it misbehave to give rootview of activity – Satyam Gondhale Aug 04 '21 at 04:51
-
"it was reported that on some devices you have to use `getWindow().getDecorView().findViewById(android.R.id.content)`" Can you specify which devices? – VIN May 25 '23 at 20:59
-
Also answer my question https://stackoverflow.com/q/76529993/4134215 – Taimoor Khan Jun 22 '23 at 11:32
This is what I use to get the root view as found in the XML file assigned with setContentView
:
final ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this
.findViewById(android.R.id.content)).getChildAt(0);

- 54,294
- 25
- 151
- 185

- 3,391
- 1
- 18
- 9
-
33This answer gave the view without the status bar - which is what I wanted. I was looking for the pixel width + height of the visible part of the activity. This one works, thanks! – Ben Clayton Mar 17 '12 at 13:17
-
9
-
5
-
3The key words here are "the root view as found in the XML file". Thank you. – Kacy Feb 28 '15 at 17:33
-
3This should be the correct marked answer. This will place the Snackbar inside your Activity (at the root), which is where it should be (I am pretty sure nobody wants to place their info behind the Nav Buttons) – Booger Sep 28 '15 at 14:50
-
-
`findViewById(android.R.id.content).getHeight();` will get you the height of the root View – vovahost Jul 19 '17 at 18:32
I tested this in android 4.0.3, only:
getWindow().getDecorView().getRootView()
give the same view what we get from
anyview.getRootView();
com.android.internal.policy.impl.PhoneWindow$DecorView@#########
and
getWindow().getDecorView().findViewById(android.R.id.content)
giving child of its
android.widget.FrameLayout@#######
Please confirm.

- 3,962
- 2
- 20
- 28
-
5
-
2Works in 4.3 Is the easiest way and the least amount of code I've found. – Oliver Dixon May 15 '14 at 00:12
-
11
-
*getWindow().getDecorView().getRootView().getHeight()* returns display height. – vovahost Jul 19 '17 at 18:27
-
4getWindow().getDecorView().getRootView() is not recommended for snack bar, it will overlap the system navigation bar, findViewById(android.R.id.content) will be better. – thanhbinh84 Aug 23 '17 at 15:48
In Kotlin we can do it a little shorter:
val rootView = window.decorView.rootView

- 2,267
- 1
- 14
- 19
-
2it, take the root of windows, if you show a snack it show the message in navigation bar – Campino Oct 03 '20 at 23:14
Just incase Someone needs an easier way:
The following code gives a view of the whole activity:
View v1 = getWindow().getDecorView().getRootView();
To get a certian view in the activity,for example an imageView inside the activity, simply add the id of that view you want to get:
View v1 = getWindow().getDecorView().getRootView().findViewById(R.id.imageView1);
Hope this helps somebody

- 380
- 3
- 12
-
7You can just call `findViewById(R.id.imageView1);` on the activity if you want the specific view. – RobCo Apr 21 '17 at 15:55
Kotlin Extension Solution
Use this to simplify access in an Activity. Then you can directly refer to rootView
from the Activity, or activity.rootView
outside of it:
val Activity.rootView get() = window.decorView.rootView
If you'd like to add the same for Fragments for consistency, add:
val Fragment.rootView get() = view?.rootView

- 42,564
- 15
- 187
- 127
For those of you who are using the Data Binding Library, to get the root of the current activity, simply use:
View rootView = dataBinding.getRoot();
And for Kotlin users, it's even simpler:
val rootView = dataBinding.root

- 130,605
- 17
- 163
- 193
-
1I think this should be upvoted especially now that data binding is the supported library for working with views. Data binding makes this process a lot simpler! – adonese Mar 25 '22 at 08:47
anyview.getRootView();
will be the easiest way.

- 7,259
- 10
- 50
- 71

- 57,232
- 27
- 203
- 212
to get View of the current Activity
in any onClick we will be getting "View view", by using 'view' get the rootView.
View view = view.getRootView();
and to get View in fragment
View view = FragmentClass.getView();

- 55
- 3
Another Kotlin Extension solution
If your activity's view is declared in xml (ex activity_root.xml
), open the xml and assign an id to the root view:
android:id="@+id/root_activity"
Now in your class, import the view using:
import kotlinx.android.synthetic.main.activity_root.root_activity
You can now use root_activity
as the view.

- 39,374
- 15
- 132
- 179
if you are in a activity, assume there is only one root view,you can get it like this.
ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this
.findViewById(android.R.id.content)).getChildAt(0);
you can then cast it to your real class
or you could using
getWindow().getDecorView();
notice this will include the actionbar view, your view is below the actionbar view

- 1,097
- 15
- 25
-
-
window.decorView or window.decorView as ViewGroup - if you need to cast it to the ViewGroup – Edhar Khimich Jan 23 '20 at 15:50