I have an activity . In that on a button click I want to change the theme but everywhere I found that theme can be set only once and that too before setContentView. Restarting an activity is not an option for me. Maybe using attrs has to do something with this problem. But I have no idea how to use it. Please Help!!
Thanks in advance!!
No that is not possible, from the relevant documentation:
So you have to set the theme with
setTheme()
before callingsetContentView()
, after that it is impossible to change it. Your only option is to recreate theActivity
.As an aside: That you say that recreating the
Activity
is not an option seems weird,Activities
have a well defined life cycle and by adhering to this life cycle it should be possible to recreate theActivity
how ever often you or the Android OS wants to. In fact you should know that yourActivity
can be killed and recreated by the Android OS at any moment and this is in fact a normal and expected behaviour of Android. So that recreating theActivity
is not an option seems to indicate a much deeper rooted problem in your apps design that will manifest itself anyway if not solved.