3

I am new to android and am trying to figure out how to change the contentView at will.

Right now, I have a TableLayout in the content view and it was set using the setContentView(TableLayout) method.

However, if I wanted to change this contentview within the same activity, how would I invoke this? Would I simply invoke the same method like: setContentView(TableLayout2) or would I need to clear the screen first?

What is the procedure for staying within the same activity and changing the content on the screen?

Thanks!

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
locoboy
  • 38,002
  • 70
  • 184
  • 260

1 Answers1

4

This answer might help.

No, you can't call it multiple times easily. You either need to entirely remove all views and then inflate the new layout, or use a ViewFlipper (or FrameLayout) to switch between different views.

Community
  • 1
  • 1
Matthew
  • 44,826
  • 10
  • 98
  • 87
  • How would I be able to remove all the views (dynamically of course)? Just so I clarify my intentions I'm trying to change the view of the activity when someone fires an event (in this case a click event). – locoboy Mar 05 '11 at 19:22
  • you can call removeAllViews() on any ViewGroup (including TableLayout) and then add new views to it. In my experience this gets fairly slow if you try to do it too often though. – Matthew Mar 05 '11 at 19:47
  • if I do this can I re-use (for lack of better terminology) the setContentView(tableLayout)? i.e. – locoboy Mar 05 '11 at 19:55
  • you would be reusing the original tableLayout, adding new rows. – Matthew Mar 05 '11 at 19:57
  • thanks. is it possible to call setContentView then removeAllViews then call setContentView again? – locoboy Mar 05 '11 at 20:24
  • I don't think so, given the answer I linked to. – Matthew Mar 05 '11 at 20:41