I would like to know if it is possible to add a view to the top of the view hierarchy for my Android App. Currently, I have a class that receives information from an external service. I would like to display information received as a custom alert to the user. Is it possible to insert this custom view at the top of the global view hierarchy, so that it shows above all other views. I would prefer not to start a new activity, just add the view on top of what is currently being shown.
Yes. Accessing the root View is as easy as finding it.
In the Activity:
FrameLayout root = (FrameLayout) findViewById(android.R.id.content);
Since the root is a
FrameLayout, you just have to add a
View to it in order for it to be on top. However, note that the Z-order will change if you add other Views after this one is added. In which case, you'll have to use
ViewGroup#bringChildToFront(View view) to bring it back to the top.
It is also possible to get the root view from another View by using View#getRootView(). This is not always reliable as the View may not be attached to the hierarchy (in which case the View itself is the root). Or it's parent may not be in the hierarchy (in which case the Parent itself will be the root).