Hector
Hector

Reputation: 13

Load layout from another XML

I want to change the content of a "RelativeLayout", the problem is that the content of that layout is in another xml.

setContentView(R.layout.activity_fullscreen);

//from actual xml
RelativeLayout rL1=  (RelativeLayout) findViewById(R.id.principalLayout); 

//from another xml 
RelativeLayout rL2=  (RelativeLayout) findViewById(R.id.cocinaL); 

rL1.removeAllViews();
rL1.addView(rL2); //this fail because rL2 is null

Thank

Upvotes: 0

Views: 53

Answers (2)

Dchaser88
Dchaser88

Reputation: 124

//from actual xml

  RelativeLayout rL1= (RelativeLayout) findViewById(R.id.principalLayout); 

//from another xml

 View child = getLayoutInflater().inflate(R.layout.child, null);
 RelativeLayout rL2 =  (RelativeLayout) child.findViewById(R.id.cocinaL); 
 rL1.removeAllViews();
 rL1.addView(rL2); 

Upvotes: 0

Ashish Patil
Ashish Patil

Reputation: 374

you can't add another layout View without LayoutInflater

    setContentView(R.layout.activity_fullscreen);


    //your actule view
    RelativeLayout rL1=  (RelativeLayout) findViewById(R.id.principalLayout); 

  LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);     
    //  your second xml
    View  view2 = inflater.inflate(R.layout.myview12, null);

    // add second View  
    rL1.addView(view2 );

Upvotes: 1

Related Questions