sirmagid
sirmagid

Reputation: 1130

android: creat layout dynamically and setContentView it dynamically

Suppose there is an Activity called "m1" and there are two layouts called "m2" and "m3" both have few buttons is dynamically how i can setContentView dynamically whay

Upvotes: 0

Views: 716

Answers (3)

Sahil Bajaj
Sahil Bajaj

Reputation: 925

The view hierarchy can have only one root. What setContentView() essentially does is that it sets the root view.

In your case,

Method 1

You'll have to either make one of the layouts as the root and add the other as a child. And call setContentView(root).

Or

Method 2

Create a dummy container layout. Set that as root. And add both your layouts as children to that container layout.

Upvotes: 1

uday
uday

Reputation: 1368

You can pass layout Resource ID or View in setContentView(); method.

try this..

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    LayoutInflater mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if(viewlayout1){
      view = mInflater.inflate(R.layout.layout1,null);
    }else{
      view = mInflater.inflate(R.layout.layout2,null);
    }
    setContentView(view);

    /*******************/

 }

Upvotes: 1

Anitha Manikandan
Anitha Manikandan

Reputation: 1170

You have a method to set view to the setContentView

So you can pass the root parent view to this method to achieve your requirement.

Upvotes: 1

Related Questions