Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setTheme not changing the theme colors

Tags:

android

themes

I know this question was asked few times. but I cant find the problem in my case. I want to change the theme of the app but my colorPrimary ,colorAccent and etc.. aren't changing.

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 made it even simpler 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 the previous questions this code should work but in my case the views that have colorPrimary in their XML still loading the old theme's colors insted of the new one even though i set the theme before calling setContentView(R.layout.activity_main);

Thanks!

like image 944
Max Avatar asked Dec 21 '16 13:12

Max


People also ask

How to change theme of app in Android?

In the Project pane select Android, go to app > res > values > themes > themes.

Which file is used to apply level theme?

You can create a theme the same way you create styles. The difference is how you apply it: instead of applying a style with the style attribute on a view, you apply a theme with the android:theme attribute on either the <application> tag or an <activity> tag in the AndroidManifest. xml file.

How to Add theme 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

If you use Fragments, they will ignore the value you have set in the onCreate(), if you override the getTheme() method, it will be used within fragments as well:

Answered on different question: Change Activity's theme programmatically

@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 it to apply. You will NOT need to change it in the onCreate anymore.

like image 115
Björn Kechel Avatar answered Oct 23 '22 05:10

Björn Kechel


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

More info here https://developer.android.com/training/material/compatibility.htmlenter image description here

like image 7
BMU Avatar answered Oct 23 '22 04:10

BMU