14

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,401
  • 15
  • 83
  • 144

2 Answers2

46

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
0
public void addContentView(View view,
              LayoutParams params) {
  mActivity.addContentView(view, params);
}

//

public static void SetActivityRoot(Activity c) {
      View v = ((ViewGroup)c.findViewById(android.R.id.content)).getChildAt(0);
    
      ScrollView sv = new ScrollView(c);
      LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
          LayoutParams.MATCH_PARENT);
      sv.setLayoutParams(lp);
    
      ((ViewGroup) v.getParent()).removeAllViews();
    
      sv.addView((View) v);
      c.addContentView(sv, lp);
    }

//

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  LinearLayout mainLayout = 
    (LinearLayout)findViewById(R.id.mainlayout);
  
  //newButton added to the existing layout
  Button newButton = new Button(this);
  newButton.setText("Hello");
  mainLayout.addView(newButton);
  
  //anotherLayout and anotherButton added 
  //using addContentView()
  LinearLayout anotherLayout = new LinearLayout(this);
  LinearLayout.LayoutParams linearLayoutParams = 
    new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.WRAP_CONTENT,
      LinearLayout.LayoutParams.WRAP_CONTENT);
  
  Button anotherButton = new Button(this);
  anotherButton.setText("I'm another button");
  anotherLayout.addView(anotherButton);
  
  addContentView(anotherLayout, linearLayoutParams);
 }

}
Suresh B B
  • 1,387
  • 11
  • 13