Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why getApplicationContext().setTheme() in a Activity does not work?

Tags:

android

themes

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);
    }
}
like image 811
user221778 Avatar asked Dec 01 '09 03:12

user221778


People also ask

When would you call getApplicationContext () and why?

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().

What is the difference between this and 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.

How do I change the theme code in Android Studio?

Switch theme:File -> Settings-> Appearance & behavior -> Appearance. Select the "theme" dropdown, and change between whatever themes you have installed.

How do I apply a theme to my android?

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.


2 Answers

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.

like image 182
Matthias Avatar answered Oct 12 '22 11:10

Matthias


you can use setTheme(..) before calling setContentView(...)and super.oncreate() and it should work fine

like image 28
Ahmed Salem Avatar answered Oct 12 '22 13:10

Ahmed Salem