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?
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.
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.
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.
Try using
ViewGroup view = (ViewGroup) findViewById(android.R.id.content);
Every layout has this ViewGroup as parent.
getWindow().getDecorView().getRootView();
set this view directly
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With