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.