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.
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.
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
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);
}
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