Dynamically add layouts

Asked

Viewed 985 times

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 this other layout I created.

My question is: how to add this custom layout to my screen inside the other layout?

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

Every time you click a button I must add the custom layout inside the linear that is on my screen.

How should I do?

  • It would be more practical to add the relevant code to the question so that an answer can be provided that meets the resolution of your problem.

1 answer

2


I’m guessing this XML you quoted is what you’re wanting to add on your screen dynamically, right?

So you will get this View, assuming it is the layout qualquer.xml, thus:

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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.