SetTheme Does Not Change Theme Colors

I know that this question has been asked several times. but I can not find the problem in my case. I want to change the theme of the application, but my color is Primary, colorAccent, etc. Do not change.

my MainActivity extends BasicActivity. it looks like this:

public class MainActivity extends BasicActivity { public static String MY_PREFS = "MY_PREFS"; private SharedPreferences mySharedPreferences; int prefMode = Activity.MODE_PRIVATE; private Toolbar toolbar; private TabLayout tabLayout; private ViewPager viewPager; private ViewPagerAdapter adapter; private TextView tabOne, tabTwo, tabThree; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } 

this is my BasicActivity (in this case, I even simplified to show that the theme is taken from R.style):

 public class BasicActivity extends AppCompatActivity { public static String MY_PREFS = "MY_PREFS"; int prefMode = Activity.MODE_PRIVATE; protected void onCreate(Bundle savedInstanceState) { JsonParser parser = new JsonParser(getApplicationContext()); int resourceId = this.getResources().getIdentifier(parser.getThemeID(), "style", this.getPackageName()); setTheme(R.style.c_2ecc71_BC6C2B); if (android.os.Build.VERSION.SDK_INT >= 19) { getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } super.onCreate(savedInstanceState); } } 

and my XML:

 <style name="c_2ecc71_BC6C2B" parent="@style/Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">#2ecc71</item> <item name="colorPrimaryDark">#1ebc61</item> <item name="colorAccent">#BC6C2B</item> </style> 

According to previous questions, this code should work, but in my case, views that have colorPrimary in their XML still load the old colors to those that were added to the new one, even if I set the theme before calling setContentView(R.layout.activity_main);

Thanks!

+7
android themes
source share
8 answers

You are trying to expand one of the new Android themes (above API 21). In addition to all the answers above, you can put your theme in styles.xml (v21).

Further information here https://developer.android.com/training/material/compatibility.html enter image description here

+3
source share

If you use fragments, they will ignore the value that you set in onCreate (), if you override the getTheme () method, it will also be used inside the fragments:

Answered another question: programmatically change the topic of activity

 @Override public Resources.Theme getTheme() { Resources.Theme theme = super.getTheme(); theme.applyStyle(R.style.c_2ecc71_BC6C2B, true); return theme; } 

Use it in your MainActivity or your BasicActivity, depending on where you want to apply it. You will no longer need to change it in onCreate.

+3
source share

Not sure if you really want to install it programmatically, but you can try the following: How to set Theme for activity at runtime? It does not work with calling setTheme before onCreate and setContentView

If you want to install it for the whole application, it might be easier / cleaner to install it in the AndroidManifest.xml file.

 <application android:theme="@style/CustomTheme"> 

In addition, I would very much avoid using a style name that has values. The point of using style is to avoid hard-coding values ​​and allow them to be customized and reused. What if you want to change colorPrimary, are you also going to change the name of the style?

+1
source share

In the manifest file, verify that the corresponding theme is activated.

+1
source share

If you want to change this type of material at run time, you must insert all of these “setTheme (android.R.style.Theme_Name)”; methods inside runonUiThread, for example:

 public class BasicActivity extends AppCompatActivity { public static String MY_PREFS = "MY_PREFS"; int prefMode = Activity.MODE_PRIVATE; protected void onCreate(Bundle savedInstanceState) { JsonParser parser = new JsonParser(getApplicationContext()); int resourceId = this.getResources().getIdentifier(parser.getThemeID(), "style", this.getPackageName()); runOnUiThread(new Runnable() { @Override public void run() { setTheme(R.style.c_2ecc71_BC6C2B); } }); recreate(); if (android.os.Build.VERSION.SDK_INT >= 19) { getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } super.onCreate(savedInstanceState); } } 

and call the rereate () function after!

According to Android -

void recreate () Explain that this object will be created with a new instance. This leads essentially to the same thread as when creating an Activity due to a configuration change - the current instance will go through its life cycle in onDestroy (), and then create a new instance after it.

+1
source share

Just change your BasicActivity and MainActivity as shown below and create an appropriate theme. You can use general preferences to check the status of the theme while adding the application.

BasicActivity.java

 public abstract class BasicActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); try { if (getLayoutID() != 0) { setContentView(getLayoutID()); } } catch (Exception ex) { /* ... */ } final boolean THEME_DARK = true;// read appropriate value from SP or any other storage Toolbar toolbar; if ((toolbar = getToolbar()) != null) { if (THEME_DARK/* check theme type here*/) { toolbar.setPopupTheme(R.style.c_2ecc71_BC6C2B); } try { setSupportActionBar(toolbar); } catch (NoClassDefFoundError e) { // Toast finish(); } } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Window window = getWindow(); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); window.setStatusBarColor(getResources().getColor(R.color.colorPrimary)); } } public abstract Toolbar getToolbar(); public abstract int getLayoutID(); } 

MainActivity.java

  public class MainActivity extends BasicActivity { private Toolbar toolbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public Toolbar getToolbar() { return toolbar == null ? toolbar = (Toolbar) findViewById(R.id.toolbar) : toolbar; } @Override public int getLayoutID() { return R.layout.activity_main; } } 
+1
source share

Your theme is hard-coded in BaseActivity , and not on the target resource identifier.

You need to put setTheme(value_from_resourceId);

Currently BaseActivity always calls regardless of the value you analyzed

 setTheme(R.style.c_2ecc71_BC6C2B); 

than refer to runtime value

+1
source share

To set a theme at runtime, you can use the following line of code:

setTheme (android.R.style.Theme_Name);

and write it before calling the setContentView () and super.onCreate () methods inside the onCreate () method.

0
source share

All Articles