i am currently have a hard time for calling to getApplicationContext().setTheme()
in a activity, I just want to apply a theme resource in a application scope instead of activity scope in code style, but the trouble is that this does not work at all, can anybody explain to this.
Thanks a lot.!
He is the code skeleton:
public class StartUp extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
/*
* setTheme(android.R.style.Theme_Black_NoTitleBar_Fullscreen);
* //that works!
*/
this.getApplicationContext().setTheme(
android.R.style.Theme_Black_NoTitleBar_Fullscreen);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
This method is generally used for the application level and can be used to refer to all the activities. For example, if we want to access a variable throughout the android app, one has to use it via getApplicationContext().
this refers to the current activity ( context ) whereas the getApplicationContext() refers to the Application class. The important differences between the two are that the Application class never has any UI associations and as such has no window token.
Switch theme:File -> Settings-> Appearance & behavior -> Appearance. Select the "theme" dropdown, and change between whatever themes you have installed.
To change default themes go to File and click on Settings. A new Settings dialog will appear, like this. Under the Appearance & Behaviour -> Appearance, you will find Theme. Choose the right theme from the drop-down and click on Apply and then Ok.
I had the same problem before and didn't find a way to fix this. Only god knows why, but I've even seen Android framework engineers (I believe it was Dianne Hackborn) say that setting themes like this is discouraged.
Set the theme for your Activity in the Manifest instead, and it will work.
you can use setTheme(..)
before calling setContentView(...)
and super.oncreate()
and it should work fine
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With