0

I'm going to get a color using volley request (I need to use volley, not sharedpreferences for this.)

the problem is to set it after my setContentView.

style.xml:

<style name="Red" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">#ff0000</item>
        <item name="colorPrimaryDark">#000000</item>
        <item name="colorAccent">#ff0000</item>
    </style>

my oncreate:

 protected void onCreate(Bundle savedInstanceState) {

        String color = "Red"; // I need to get this color using volley, so I cannot set red here, but if I don't set it here, it will not work.

        Utils.setThemeToActivity(this, color); // same

        super.onCreate(savedInstanceState);


        setContentView(R.layout.name);
    }

it is working because I set color as red. but I need to get this red from volley so I cannot put it in the first line of oncreate as it is now.

any ideas how can I pass red to Utils.setTheme there?

my Utils:

public class Utils {

    public static String SIZE="";
    public static boolean settingChanged=false;
    public static String THEME="";

    public static void setThemeToActivity(Activity act, String color ) {
        try {
            if(Utils.THEME.equalsIgnoreCase("Yellow")) {
                act.setTheme(R.style.Yellow);
            }
            if(Utils.THEME.equalsIgnoreCase("Red")) {
                act.setTheme(R.style.Red);
            }

            if(Utils.THEME.equalsIgnoreCase("Blue")) {
                act.setTheme(R.style.Blue);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
RGS
  • 4,062
  • 4
  • 31
  • 67

2 Answers2

2

You can use as follows -

Theme theme = super.getTheme();
theme.applyStyle("Your style");

Check this answer - https://stackoverflow.com/a/39150319/1649353

Community
  • 1
  • 1
Ambuj Kathotiya
  • 369
  • 4
  • 18
2

If you want to change theme of an already existing activity, call recreate() after setTheme().

like

Your webservice response then call

Utils.setThemeToActivity(this, color); // same

this.recreate()

if you wanna to do something in recreate then Override this method otherwise no need to write it
@Override
public void recreate() {
    super.recreate();


}

Try 2nd solution

 public static void setThemeToActivity(Activity act, String color ) {
    try {
        if(Utils.THEME.equalsIgnoreCase("Yellow")) {
            act.setTheme(R.style.Yellow);
        }
        if(Utils.THEME.equalsIgnoreCase("Red")) {
            act.setTheme(R.style.Red);
        }

        if(Utils.THEME.equalsIgnoreCase("Blue")) {
            act.setTheme(R.style.Blue);
        }

        act.finish();
        act.startActivity(new Intent(act, activity.getClass()));
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
  • it seems like a loop when I tried to open myclass. I add this on utils: `act.finish(); act.startActivity(new Intent(act, act.getClass())); ` act instead of activity, is it right? maybe the problem is that it is not getting the color on `String color`? – RGS Mar 01 '17 at 12:34
  • 1
    act is your Activity act params which is *setThemeToActivity(Activity act, String color )* – Dharmbir Singh Mar 01 '17 at 12:40
  • the `this.recreate()` is adding a loop to my activity – RGS Mar 01 '17 at 13:05
  • 1
    Yes, thats the issue, now you have to make a global variable and don't call your all method in your activity if it is true . – Dharmbir Singh Mar 01 '17 at 13:06
  • thank you! could you give me an example of this so you can solve my problem? thank you very much again! – RGS Mar 01 '17 at 13:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/136960/discussion-between-dharmbir-singh-and-rick-joe). – Dharmbir Singh Mar 01 '17 at 13:13