Get root view from current activity
Asked Answered
X

12

729

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?

Xanthochroism answered 20/12, 2010 at 0:40 Comment(3)
In 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.Quiberon
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 !Xanthochroism
@xandy: a slight typo: XAML -> XML.Czarevitch
K
1172

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.

Killion answered 20/12, 2010 at 8:58 Comment(15)
Actually 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.Xanthochroism
You can check here I suppose 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.Killion
I'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.Biota
@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.Erythro
@Erythro I need to use .getRootView() in Android 5.0+ when using action barFishbowl
When 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.Straphanger
@nickes,how to get the rootview of fragment??Autotruck
@VikramSingh call getView() in Fragment.Craggie
@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.]Transmundane
activity.findViewById(android.R.id.content) is returning null in Android 5. :(Risteau
@JuanJoséMeleroGómez Is it consistent for all Android 5 devices or is it only for your device?Killion
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.Risteau
Perfect working. Without this approach it misbehave to give rootview of activitySupertax
"it was reported that on some devices you have to use getWindow().getDecorView().findViewById(android.R.id.content)" Can you specify which devices?Gabrielagabriele
Also answer my question https://mcmap.net/q/64480/-add-view-to-android-rootview-application-without-permission/4134215Stutter
K
280

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);
Klayman answered 21/2, 2011 at 17:48 Comment(7)
This 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!Biota
This excludes ActionBar!False
Is there a way to include the ActionBar as well?Quince
The key words here are "the root view as found in the XML file". Thank you.Relic
This 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)Straphanger
why is the use of getChildAt(0) ? Anyone?Cookery
findViewById(android.R.id.content).getHeight(); will get you the height of the root ViewUel
D
150

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.

Desma answered 19/7, 2012 at 3:38 Comment(5)
Android2.3.3 seems sameAusterlitz
Works in 4.3 Is the easiest way and the least amount of code I've found.Ambit
Best if you're using SnackBarWartburg
getWindow().getDecorView().getRootView().getHeight() returns display height.Uel
getWindow().getDecorView().getRootView() is not recommended for snack bar, it will overlap the system navigation bar, findViewById(android.R.id.content) will be better.Badmouth
D
37

Get root view from current activity.

Inside our activity we can get the root view with:

ViewGroup rootView = (ViewGroup) ((ViewGroup) this
            .findViewById(android.R.id.content)).getChildAt(0);

or

View rootView = getWindow().getDecorView().getRootView();
Dodecanese answered 29/11, 2016 at 20:23 Comment(0)
F
35

In Kotlin we can do it a little shorter:

val rootView = window.decorView.rootView
Formless answered 25/10, 2018 at 9:15 Comment(1)
it, take the root of windows, if you show a snack it show the message in navigation barGuyon
B
22

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

Bluepoint answered 26/3, 2016 at 0:4 Comment(1)
You can just call findViewById(R.id.imageView1); on the activity if you want the specific view.Conditioning
F
11

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
Font answered 22/3, 2020 at 2:43 Comment(0)
S
8

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
Smoothen answered 21/5, 2020 at 10:16 Comment(1)
I 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!Cantilever
L
5

anyview.getRootView(); will be the easiest way.

Lucid answered 13/5, 2018 at 18:49 Comment(0)
P
1

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();

Pesky answered 12/4, 2018 at 7:43 Comment(0)
R
1

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.

Rotman answered 3/6, 2020 at 19:28 Comment(0)
A
0

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

Argus answered 28/7, 2016 at 9:2 Comment(2)
There is no such method in Activity class.Epiphytotic
window.decorView or window.decorView as ViewGroup - if you need to cast it to the ViewGroupInquietude

© 2022 - 2024 — McMap. All rights reserved.