3

I am creating a simple examination result storage program. The activity changes theme depending on the user's current result - for example blood red for a failing grade e.t.c. When a user deletes a subject and it causes a significant change in the overall result, the theme changes. The theme is decided by this method:

    private int getTheme()
        {

            String[] themes={"GreenTheme","TurquoiseTheme","OrangeTheme","RedTheme","BlackTheme"};
            int index=0;
            float gpa =getSemesterGpa(currentSemester);

                if (gpa >= 3.5)
                    index = 0;
                else if (gpa >= 3)
                    index = 1;
                else if (gpa >= 2.5)
                    index = 2;
                else if (gpa >= 2)
                    index=3;
                else
                    index=4;


            return getResources().getIdentifier(themes[index],"style",getPackageName());
        }

And I have in my onCreate():

protected void onCreate(Bundle savedInstanceState)

    {        
        setTheme(getSgpaTheme());
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_semester_detail);
        ...
    }

Whenever a subject deletion causes a major change in the GPA, I use recreate() so that the activity is reloaded with the new theme. As I understand it, there is no other way to apply the new theme dynamically. Unfortunately, recreate() tends to produce a jarring flicker of the screen. On one device, a Samsung S9+, the screen even goes black for a moment. But on my S7 and Huawei device there is only a flicker, still very annoying.

So the question is: is there a way to achieve this, with or without recreate(), which would allow me add a nice fade-in transition as the colors change.

P.S: This is my first question so please excuse if I have provided too little information. I wasn't sure what was relevant. Also, I have only been programming in Java and Android-Studio for about 3 weeks, so I request a noob-oriented answer. Thank you.

1 Answers1

0

modify the below code according to need

 @Override
    public Resources.Theme getTheme() {

        Resources.Theme theme=super.getTheme();

        preferencesTheme=getSharedPreferences("themes", Context.MODE_PRIVATE);
        String themeName=preferencesTheme.getString("theme","orange");

        if("orange".equals(themeName)){
             theme.applyStyle(R.style.OrangeTheme,true);
        }else if("red".equals(themeName)) {
             theme.applyStyle(R.style.RedTheme,true);
        }

        return theme;
    }

than in button click

@Override
    public void onClick(View v) {

        SharedPreferences.Editor mEditorTheme=preferencesTheme.edit();

        if(v.getId()==R.id.rdoOrange ){

            mEditorTheme.putString("theme","orange");
            mEditorTheme.apply();
            recreate();

        }else if(v.getId()==R.id.rdoRed){

            mEditorTheme.putString("theme","red");
            mEditorTheme.apply();
            recreate();
        }
    }

in style.xml

 <style name="OrangeTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">#FF5722</item>
        <item name="colorPrimaryDark">#B13C17</item>
        <item name="colorAccent">#8BC34A</item>
    </style>

    <style name="RedTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">#F44336</item>
        <item name="colorPrimaryDark">#A00B0B</item>
        <item name="colorAccent">#FF9800</item>
    </style>

I hope this will help you.

Lokik Soni
  • 602
  • 1
  • 5
  • 25
  • Thanks for the quick reply. I tried to read the documentation you linked, but i just felt i lacked too much context to even comprehend what was being communicated. While i intend to familiarize myself with the depth of these things eventually, for now can you just noobify the approach for me. I'll be really grateful. Thanks. – Someone Curious Jul 17 '19 at 08:51
  • That would be ideal if you can. Thanks. – Someone Curious Jul 17 '19 at 09:03
  • hey @LokikSoni how does the setTheme(name) method look like? – Pemba Tamang Jul 17 '19 at 09:45