-2

I can set the application's theme in the manifest, like so:

<application>
     android:theme="@style/AppTheme.NoActionBar"
<!-- Activities here -->
</application>

I also know how to set the theme of an activty both in the manifest, and programmatically.

What I would like to know is how to set the entire applications's theme programmatically (I plan to allow the user to select between multiple themes).

One solution would be to apply the selected theme to all activities in the onCreate methods, but I have quite a few activities. Is there a simpler solution?

Gtomika
  • 845
  • 7
  • 24
  • Possible duplicate of [Switching application-wide theme programmatically?](https://stackoverflow.com/questions/4663752/switching-application-wide-theme-programmatically) – Ricardo A. Oct 11 '19 at 20:19

1 Answers1

3

As you can see in this post ,it's impossible to set app whole theme as you expect. But you can set theme in a setting Activity using a SharedPreference object and Apply your theme in every Activity that you want:

//A method that return your styles id
int style_var=getStyle();

SharedPreferences.Editor editor 
=getSharedPreferences("mypref", 
MODE_PRIVATE).edit();
editor.putInt("idName", style_var); 
editor.apply();

And in Any Activity implement this piece of code before super.onCreate(savedInstanceState):

SharedPreferences prefs = 
getSharedPreferences("mypref", 
MODE_PRIVATE);
int styleId = prefs.getInt("idName", 
R.style.defaultStyle);
//set Activity theme 
setTheme(styleId);

Update: And to avoid duplicating code , Create a custom Activity class like this:

 public class myBaseActivity extends Activity{

@Override
public void onCreate(Bundle savedInstanceState)
{
    SharedPreferences prefs = 
        getSharedPreferences("mypref",MODE_PRIVATE);
    int styleId = prefs.getInt("idName",R.style.AppTheme);
    //set Activity theme 
    setTheme(styleId);
    
    super.onCreate(savedInstanceState);
}
}

and simply extend your Activities from the custom Activity instead of Activity:

public class AnyActivity extends 
myBaseActivity{
@Override
public void onCreate(Bundle 
savedInstanceState)
{
super.onCreate(savedInstanceState);
    setContentView(R.layout.any);
}
}

and finally in your setting Activity ,implement this piece of code to reload the app(for example put it in a Onclick method of a "Save and Reload" button ):

Intent intent = new Intent(SettingActivityClass.this, YourAppMainActivity.class);
//replace YourAppMainActivity with SettingActivityClass if you want to stay in setting activity on reload
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                    Intent.FLAG_ACTIVITY_CLEAR_TASK |
                    Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);

I hope this helps.

ahooee
  • 275
  • 3
  • 14
  • So I must apply theme in every activity separately. This is what I was afraid of... Well I guess creating a base activity and subclass my activities from that one could work. Anyways, I will mark your answer accepted soon if no one else suggests something better. – Gtomika Oct 11 '19 at 17:32
  • See the updated answer ... Maybe that would be useful for you – ahooee Oct 11 '19 at 23:29
  • I am trying to do [something similar](https://stackoverflow.com/q/73612480/3692177) to this question. Ok, maybe I figure out howto group all my styles' mess into a single one... then apply your solution... but only when I create the Activity! What if, with the activity already running, I wanted to switch the theme in real time? Would `onCreate` do the trick? I don't think so... then what? – SebasSBM Sep 07 '22 at 07:44