Switching Application-Wide Theme Programmatically

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

Changing Theme in Windows 10 UWP App Programmatically

Updated answer with what I finally decided on.

I used a settings class that holds all of the apps settings including what theme to use. Since the theme can only be set when it starts we need to make sure to set it them. This is the code I used:

In the App.xaml.cs file:

public App()
{
//Load settings
AppSettings.LoadSettings();
this.RequestedTheme = AppSettings.SelectedTheme;

this.InitializeComponent();
}

In the App.xaml file make sure to remove this property:

    RequestedTheme="Light"

If its not removed it always default to light with no way to change it.

This way the user can choose the theme, it gets stored and used when the app starts. Just make sure to load it and apply it in the app initialization phase.

How to set the entire application's theme programmatically?

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.

Android - How to switch theme on runtime

As you can see theme is setting on the onCreate() and before setContentView(). So you should call the oncreate() method again when you want to change the theme. But the onCreate() function will be called only once in the life cycle of an Activity.

There is a simple way to do this. I am not sure that it is the best way.

Suppose you want to apply new theme to Activity 1 on a button click.

inside the onClick event

  1. Save the theme to be applied such that it should be retained even after the application restart (preferences or static volatile variables can be used).
  2. Finish the current activity (Activity 1) and call a new activity (Activity 2).

Now in Activity 2

  1. Call Activity 1 and finish current activity (Activity 2).

In Activity 1

  1. Apply the saved theme inside onCreate.

Hope it is not confusing.. :)

Change theme at runtime

Here is an article that will walk you through it:

http://svetoslavsavov.blogspot.com/2009/07/switching-wpf-interface-themes-at.html

Basically you need to remove the "old" theme from the resource dictionary and then merge in the new one. The above article shows you how to make this change very simple.

Change Activity's theme programmatically

As docs say you have to call setTheme before any view output. It seems that super.onCreate() takes part in view processing.

So, to switch between themes dynamically you simply need to call setTheme before super.onCreate like this:

public void onCreate(Bundle savedInstanceState) {
setTheme(android.R.style.Theme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}


Related Topics



Leave a reply



Submit