13

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?

Rafael T
  • 15,034
  • 14
  • 80
  • 142

1 Answers1

45

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.

  • 4
    Wow. Really appreciate your answer, Thanks. your tip and the tool you suggested will save me a lot of hours! You are the kind of user that makes Stackoverflow so great! – Rafael T Apr 02 '12 at 16:22
  • hierarchiviewer is not deprecated but there is a option https://developer.android.com/studio/profile/hierarchy-viewer – Ivandro Jao Nov 24 '18 at 11:49