Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get root ViewGroup from a layout that was set using setContentView?

Tags:

I cannot seem to find a way to get this ViewGroup. Not sure if there are any standard ways to get ViewGroups.

What I want to accomplish is to dynamically add a full screen overlay view to any Layout at run time, and in order to do this, I always need to get the root ViewGroup. Is this the right way to do this?

like image 970
mskw Avatar asked Jul 07 '14 13:07

mskw


People also ask

How do I get root view of activity?

anyview. getRootView(); will be the easiest way. Show activity on this post. in any onClick we will be getting "View view", by using 'view' get the rootView.

What is content view in Android?

Actually setContentView() is a method part of android. app. Activity class. It helps to set our content or render our layout on the screen. Based on the value given by the user, views will be inflated and rendered after the measurement of the screen, root view, and its child views.

When should I call setContentView?

No it is not necessary to call setContentView() method. It is call to show the UI but in your case you just want to finish the activity without showing the UI, So it is fine.


2 Answers

Try using

ViewGroup view = (ViewGroup) findViewById(android.R.id.content);

Every layout has this ViewGroup as parent.

like image 193
vRallev Avatar answered Oct 11 '22 20:10

vRallev


getWindow().getDecorView().getRootView();

set this view directly

like image 43
Yogesh Rathi Avatar answered Oct 11 '22 20:10

Yogesh Rathi