On Button click change the theme of whole application dynamically and store it
You need to persist your theme somewhere. You can use SharedPreferences for it, for example. Than retreive your theme and set it to Activity before setContentView.
So. you need to add saving to changeToTheme(Activity activity, int theme)
and retreiving to onActivityCreateSetTheme(Activity activity)
How to change current Theme at runtime in Android
I would like to see the method too, where you set once for all your activities. But as far I know you have to set in each activity before showing any views.
For reference check this:
http://www.anddev.org/applying_a_theme_to_your_application-t817.html
Edit (copied from that forum):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Call setTheme before creation of any(!) View.
setTheme(android.R.style.Theme_Dark);
// ...
setContentView(R.layout.main);
}
Edit
If you call setTheme
after super.onCreate(savedInstanceState);
your activity recreated but if you call setTheme
before super.onCreate(savedInstanceState);
your theme will set and activity
does not recreate anymore
protected void onCreate(Bundle savedInstanceState) {
setTheme(android.R.style.Theme_Dark);
super.onCreate(savedInstanceState);
// ...
setContentView(R.layout.main);
}
I want to change the theme of my whole application
Here is the nice tutorial for changing the theme on Button click
and also Here is the tutorial for the Styling Android With Defaults
Switching application-wide theme programmatically?
Create a base activity for your app and override onCreate
to set the theme. Derive all your other activities from this base activity.
Also check this tutorial:
http://www.androidengineer.com/2010/06/using-themes-in-android-applications.html
Related Topics
Android Studio Debugger Highlights the Wrong Lines
What Happens When I Throw a C++ Exception from a Native Java Method
Aes Java Encoding, Ruby Decoding
Createprocess Error=206, the Filename or Extension Is Too Long When Running Main() Method
How to Do a Junit Assert on a Message in a Logger
What Is a Class Literal in Java
How to Convert a Map to List in Java
Junit Testing with Simulated User Input
How to Add Two Java.Lang.Numbers
Why I Cant Use the Populateviewholder Override Method
Java Trayicon Using Image with Transparent Background
Calculating Distance Between Two Points, Using Latitude Longitude
Java Count Occurrence of Each Item in an Array
Java 8: Lambda-Streams, Filter by Method with Exception
Java Function for Arrays Like PHP's Join()
Covariance, Invariance and Contravariance Explained in Plain English