Question

I am trying to create an option in my code to change layouts using preferences. I already have the two layouts created in XML, but I can't figure out how to swap between the two during runtime. I would like to cause it to check during onResume() since it is called directly after returning from the Preferences screen and when starting up, however I can't figure out the code necessary. I tried just using setContentView() a second time, but it didn't seem to work. What can I do? Thanks in advance!

Was it helpful?

Solution

I'm guessing you would need to save the user's preference, then have them close and restart your app, as I believe the only time you can call setContentView() is in onCreate(). You could read the layout preference as the first thing you do in onCreate() just before you call setContentView()

An option to keep from having the user restart the app might be to create a new instance of your activity once they have changed their preference, and close the current activity.

OTHER TIPS

there's a good article on dynamic layout loading here: http://tech.chitgoks.com/2008/03/19/android-how-to-load-layout-xml-files-dynamically-during-runtime/ Not exactly what you asked but might help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top