Reputation: 167
How can I change the the theme of my Android application from light to dark programmatically? I've tried something like:
setTheme(R.style.Holo_Theme_Light);
but I found nothing which worked for me.
Upvotes: 1
Views: 293
Reputation: 3682
Should be the first line in onCreate, before calling super.onCreate(savedInstanceState);
as it is where view processing takes place and your change should be before that to be included in view creation
public void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Holo_Theme_Light);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
Also, please refer to docs to know where to call your setTheme
Upvotes: 2
Reputation: 14820
See the answer here
It says
As docs say you have to call setTheme before any view output. It seems that super.onCreate() takes part in view processing.
So, to switch between themes dynamically you simply need to call setTheme before super.onCreate like this:
public void onCreate(Bundle savedInstanceState) {
setTheme(android.R.style.Theme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
So, make sure you have put setTheme(R.style.Holo_Theme_Light);
before super.onCreate(savedInstanceState);
in your code
Upvotes: 0
Reputation: 39
You can take a look here: Android - Change app Theme on onClick I am sorry it's not a comment but I don't have enough reputation :(
EDIT - You can't use the setTheme before the superoncreate, if you will call it before the superoncreate it will work, like here: Change Activity's theme programmatically
public void onCreate(Bundle savedInstanceState) {
setTheme(android.R.style.Theme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
Upvotes: 0