Add layouts dynamically

4

I created an XML with LinearLayout, in it I have a Spinner and an EditText. On my screen I have a LinearLayout that should receive that other layout I created.

My question is: how can I add this custom layout to my screen within the other layout?

I've tried using findById to get the custom layout, but it returns null.

Whenever I click on a button I should add the custom layout within the linear one that is on my screen.

How do I do it?

    
asked by anonymous 01.03.2014 / 18:37

1 answer

2

I'm guessing that this XML you mentioned is what you're trying to add to your screen dynamically, right?

So you're going to get this View , assuming it's the qualquer.xml layout, like this:

View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.qualquer, null);

And then add on your screen, for example, inside the element foo :

LinearLayout foo = (LinearLayout)findViewById(R.id.foo);
foo.addView(view);

Make sure you agree with what you need.

    
02.03.2014 / 12:07