680

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?

manfcas
  • 1,855
  • 6
  • 28
  • 46
Lalith
  • 17,686
  • 14
  • 42
  • 54
  • 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. – 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
  • 4
    @xandy: a slight typo: XAML -> XML. – superjos Jan 18 '12 at 12:53

12 Answers12

1107

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.

Googlian
  • 4,180
  • 3
  • 27
  • 29
Dmitry Ryadnenko
  • 20,952
  • 4
  • 38
  • 52
  • 201
    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. – Lalith Dec 21 '10 at 06:39
  • 5
    You 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
  • 14
    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. – Ben Clayton Mar 17 '12 at 13:16
  • 5
    @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
  • 1
    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. – Booger Sep 28 '15 at 14:31
  • @nickes,how to get the rootview of fragment?? – Reprator Dec 30 '15 at 11:52
  • @VikramSingh call `getView()` in Fragment. – Yaroslav Mytkalyk Mar 30 '16 at 08:09
  • @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
273

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);
Jared Burrows
  • 50,718
  • 22
  • 143
  • 180
pottedmeat
  • 3,321
  • 1
  • 15
  • 9
145

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.

Sudar Nimalan
  • 3,842
  • 2
  • 17
  • 27
35

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();
Community
  • 1
  • 1
Jorgesys
  • 114,263
  • 22
  • 306
  • 247
28

In Kotlin we can do it a little shorter:

val rootView = window.decorView.rootView
Artem Botnev
  • 1,370
  • 1
  • 9
  • 15
19

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

Salah Klein
  • 350
  • 3
  • 12
  • 5
    You can just call `findViewById(R.id.imageView1);` on the activity if you want the specific view. – RobCo Apr 21 '17 at 15:55
7

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
Gibolt
  • 24,018
  • 9
  • 129
  • 89
4

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

Arnaud
  • 6,339
  • 8
  • 47
  • 64
Khemraj Sharma
  • 46,529
  • 18
  • 168
  • 182
3

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
Alex Mamo
  • 91,677
  • 13
  • 105
  • 138
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();

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.

smac89
  • 26,360
  • 11
  • 91
  • 124
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

bowman han
  • 1,010
  • 12
  • 22