1

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.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
user3531864
  • 167
  • 3
  • 11

3 Answers3

2

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

user3487063
  • 3,672
  • 1
  • 17
  • 24
0

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);
}
Community
  • 1
  • 1
David
  • 39
  • 4
0

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

Community
  • 1
  • 1
Lal
  • 14,726
  • 4
  • 45
  • 70