tags:

views:

473

answers:

3

hi all,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);
}

}

A: 

When do you call setTheme() on your application context? It must be called before instantiating any views.

Mads Kristiansen
Thank you for your answer,I do call the method before instantiating any viwes,but still doesn't work..
A: 

I didn't try this out myself, but if it was absolutely necessary to set the theme programatically, the next thing I'd try would be to derive a class from Application and override the onCreate method like in the following:

public class MyApplication extends android.app.Application {
    @Override
    public void onCreate() {
        super.onCreate();
        setTheme(android.R.style.Theme_Black_NoTitleBar_Fullscreen);

    }
}
Mads Kristiansen
A: 

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.

Matthias