Android – Activity.addContentView(View) == ViewGroup.addContentView(View)

androidandroid-activityviewviewgroupwindow

I have a question regarding Android Activitys:

An Activity has the Method addContentView(View) while a ViewGroup has a (similar?) addView(View) Method.

Unfortunately its undocumented where the View from addContentView is placed. Is it like a LinearLayout just adding the View to the bottom, or is it more like a FrameLayout, which adds its Views "onTop" ? Does it depend on the ViewGroup set by setContentView?

If I dive into the sources I see that addContentView will call Window's abstract Method addContentView. Unfortunately I cannot see which class is implementing this Method. So whats the behaviour of Activitys addContentView exactly?

Best Answer

The base layout of every activity is a FrameLayout. This means the layout you usually set via setContentView() is a child of this layout. addContentView() adds just another child, therefore it behaves like a FrameLayout (which means it adds new UI elements above existing ones).

You can check this by using a tool called hierachyviewer from your ANDROID_SDK\tools folder. Here are two screenshots:

enter image description here

This is the layout before calling addContentView(), my activity consists of the default FrameLayout, holding a LinearLayout with a Button (my layout here). This is reflected in the bottom row here, the other elements above are the title/statusbar.

enter image description here

After adding a TextView via addContentView() it looks like this. You can see that the base FrameLayout got a new child.