Android: new GUI question - how to declare a viewGroup without an XML layout file?

In the application, I'm afraid I have a custom view. I cannot declare it in the layout XML file, because I will use it from an action that contains my custom instance of the view, and I need to have access to it (I cannot override findViewById ...).

In this, I decided to declare all the GUI elements in the Activity.

But I just can not take a single step forward, since I can not even create an instance of viewGroup ...

Here is what I am trying:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ViewGroup vg = new ViewGroup(this.getApplicationContext());

    setContentView(vg);

}

and I get "Unable to instantiate ViewGroup" ...

Can someone give a direct example on how to declare a viewGroup that contains views?

... XML ...?

, !

+5
1

[ViewGroup][1] - , . , . , LinearLayout of RelativeLayout ViewGroup. , - :

   @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  LinearLayout vg = new LinearLayout(this);
// set the LayoutParams the way you want.
// and add textviews, imageviews, ... here for instance.
  setContentView(vg);

 }

LayoutParams, , LayoutParams.Fill_parent

+7

All Articles