3

I want dynamically change the theme of my app with buttons, so I implemented this:

 sharedPreferences = getSharedPreferences("VALUES",MODE_PRIVATE);
    int theme = sharedPreferences.getInt("THEME",2);

    switch (theme){
        case 1: setTheme(R.style.AppTheme);
            break;
        case 2: setTheme(R.style.AppTheme_AppBarOverlay);
            break;
    }

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tutotial);

And this is the code of the buttons:

        tb1 =(Button) findViewById(R.id.button2);

    tb1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            sharedPreferences.edit().putInt("THEME",1).apply();
            Intent intent = new Intent(tutotial.this, tutotial.class);
            startActivity(intent);
        }
    });

tb2 =(Button) findViewById(R.id.button3);

    tb2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            sharedPreferences.edit().putInt("THEME",2).apply();
            Intent intent = new Intent(tutotial.this, tutotial.class);
            startActivity(intent);
            Intent intent1 = new Intent(tutotial.this, MainActivity.class);
            startActivity(intent1);

        }
    });

The problem is that code just changes the theme of the activity associated and it does not make the change theme in all the app.

Richard Erickson
  • 2,568
  • 8
  • 26
  • 39

3 Answers3

1

There is an open source podcast player called AntennaPod on github. It contains example code that does this.

The way they do it is to call ContextThemeWrapper.setTheme(int) at the beginning of each Activity.onCreate() method.

@Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(UserPreferences.getTheme());
    super.onCreate(savedInstanceState);
    ......
}

This could be done in each activity, or by creating a base activity that does this for you on each subclass.


On closer reading of your question, this is exactly what you are doing. So I would say you are on the right track.


It also seems this has been asked before:

All offering the same solution.

Community
  • 1
  • 1
Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255
0

You can apply a theme to any activity by including android:theme inside activity inside manifest file.

For example:

<activity android:theme="@android:style/Theme.Dialog">
<activity android:theme="@style/CustomTheme">

And if you want to set theme programatically then use setTheme() before calling setContentView() and super.onCreate() method inside onCreate() method.

Fakher
  • 2,098
  • 3
  • 29
  • 45
  • I want to set theme programatically and i did that you told me. The poroblem is the code i implemented just set the theme of one activiy and i want to set all the activities. I have two themes in styles.xml, one per each button – Fernando Luján Martínez Nov 07 '15 at 17:37
  • that's excatly what i said. in each tag put the theme that you want, or another solution is to set your theme before setContentView() in each of your activities – Fakher Nov 07 '15 at 22:01
-1

You can do this by implementing various themes and on click of button change the themes accordingly, refer this change theme programatically for assistance.

Swapnil Kadam
  • 4,075
  • 5
  • 29
  • 35