Changing themes in java does not change background color

I am trying to change the theme at runtime using java code because I want the user to be able to change the theme of the application through the settings menu. so I allow the user a topic, and then read the results as follows:

if (...) { getApplication().setTheme(R.style.BlackTheme); } else { getApplication().setTheme(R.style.LightTheme); } 

Unfortunately, this does not work for some reason .... the font color is slightly shifted from a darker gray (light theme) to a lighter gray (black theme) but the background always remains white / black (depending on which The theme was originally selected in the manifest file)

If I completely delete the topic-entry in the manifest file, then it is as if I have selected the black theme ....

.... is there something I'm missing?

+4
source share
3 answers

I had the same problem and solved it this way.

 @Override public void onCreate(Bundle savedInstanceState) { if (getIntent().hasExtra("bundle") && savedInstanceState==null){ savedInstanceState = getIntent().getExtras().getBundle("bundle"); } //add code for theme switch(theme) { case LIGHT: setTheme(R.style.LightTheme); break; case BLACK: setTheme(R.style.BlackTheme); break; default: } super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //code } 

this code is designed to recreate a downturn and change the theme. You must write your own onSaveInstanceState (Bundle outState); From API-11 you can use recreate () method instead

 Bundle temp_bundle = new Bundle(); onSaveInstanceState(temp_bundle); Intent intent = new Intent(this, MainActivity.class); intent.putExtra("bundle", temp_bundle); startActivity(intent); finish(); 
+1
source

You cannot install the application theme at runtime. If you want to change the theme throughout your application, you must call

setTheme (Resid)

as the first thing in Activity onCreate ().

For instance:

@Override public void onCreate (Bundle savedInstanceState) {setTheme (resId)}

If you want to change the theme of already running actions, you need to recreate them.

0
source

This is a known issue: https://code.google.com/p/android/issues/detail?id=3793

You must manually change the background color, setTheme() will not change it.

0
source

All Articles