Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android change setContentView

I want to dynamically set the contentView in my activity. Because one time i use a xml as contentView, but at anothertime i use a custom view as contentView.

But how do i change the contentView?

I read about that the ViewFlipper can do this, but a viewFlipper is implemented in a xml file. And within this ViewFlipper you can add your different views.

But i dont know them at the beginning, so i cant write them all in my xml file. Do you have any idea?

Thank you

like image 559
krackmoe Avatar asked Jan 01 '12 17:01

krackmoe


1 Answers

You can call setContentView at any time*, not just in onCreate. Just define all the views you want in separate XML files and pass the relevant id when it's time to switch. If you want to define the new layout dynamically in code, then do that and call setContentView and pass the root view of your new layout.

* Technically, you can call setContentView any time you are executing on the event thread. Otherwise you need to use a Handler to call it.

like image 196
Ted Hopp Avatar answered Oct 07 '22 13:10

Ted Hopp