Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setTheme to an activity at runtime? It doesn't work call setTheme before onCreate and setContentView

Tags:

I want setTheme to an activity at runtime, I have search some solutions by google. someone said call setTheme before onCreate and setContentView can works, the code section like

public void onCreate(Bundle savedInstanceState) {     setTheme(android.R.style.Theme_Translucent_NoTitleBar);     super.onCreate(savedInstanceState);     ...     setContentView(...) } 

but it not works, I want to know, is there another solution can setTheme to activity?

like image 964
herman brain Avatar asked Jun 21 '12 09:06

herman brain


People also ask

Why is onCreate called twice?

OnCreate will only be called one time for each lifetime of the Activity. However, there are a number of situations that can cause your activity to be killed and brought back to life. Thus, onCreate will be called again.

Which object is passed to onCreate () method?

The savedInstanceState is a reference to a Bundle object that is passed into the onCreate method of every Android Activity. Activities have the ability, under special circumstances, to restore themselves to a previous state using the data stored in this bundle.

How many times onCreate is called?

OnCreate is only called once.


2 Answers

Just try this - set your theme after super.onCreate(savedInstanceState); and before setContentView(...)

Like below code -

public void onCreate(Bundle savedInstanceState)  {     super.onCreate(savedInstanceState);     setTheme(android.R.style.Theme_Translucent_NoTitleBar); // Set here     setContentView(...) } 
like image 74
Praveenkumar Avatar answered Sep 30 '22 08:09

Praveenkumar


Actually this only worked for me if I set it before calling super.onCreate(savedInstanceState);

public void onCreate(Bundle savedInstanceState) {     final int themeRes = getIntent().getIntExtra(EXTRA_THEME_ID, 0);     if (themeRes != 0) setTheme(themeRes);     super.onCreate(savedInstanceState);     //ect... } 
like image 23
Chris.Jenkins Avatar answered Sep 30 '22 06:09

Chris.Jenkins