Get root view from current activity

AndroidAndroid ActivityView

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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

Solution 3 - Android

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.

Solution 4 - Android

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

Solution 5 - Android

In Kotlin we can do it a little shorter:

val rootView = window.decorView.rootView

Solution 6 - Android

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

Solution 7 - Android

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

Solution 8 - Android

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

Solution 9 - Android

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

Solution 10 - Android

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

Solution 11 - Android

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.

Solution 12 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLalithView Question on Stackoverflow
Solution 1 - AndroidDmitry RyadnenkoView Answer on Stackoverflow
Solution 2 - AndroidpottedmeatView Answer on Stackoverflow
Solution 3 - AndroidSudar NimalanView Answer on Stackoverflow
Solution 4 - AndroidJorgesysView Answer on Stackoverflow
Solution 5 - AndroidArtem BotnevView Answer on Stackoverflow
Solution 6 - AndroidSalah KleinView Answer on Stackoverflow
Solution 7 - AndroidGiboltView Answer on Stackoverflow
Solution 8 - AndroidAlex MamoView Answer on Stackoverflow
Solution 9 - AndroidKhemraj SharmaView Answer on Stackoverflow
Solution 10 - Androiduser3509903View Answer on Stackoverflow
Solution 11 - Androidsmac89View Answer on Stackoverflow
Solution 12 - Androidbowman hanView Answer on Stackoverflow