Search code examples
androidandroid-preferencesandroid-theme

Android: How to I apply a theme to all the activities of application after selecting one from preferences?


I have made a simple app and i want to change the theme of all the activities of the app (including the settings activity) after selecting from the preference but the themes do not apply after selection. I've tried adding a recreate() but gets stuck in trying to start the main activity. For now, i added a menu item that does recreate() but i want it to apply automatically once you leave the settings activity.

MainActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {

    SharedPreferences getData = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    String themeValues = getData.getString("theme_preference", "1");

    if (themeValues.equals("1")) {
        setTheme(R.style.Theme_Light);
    }

    if (themeValues.equals("2")) {
        setTheme(R.style.Theme_Dark);
    }

    if (themeValues.equals("3")) {
        setTheme(R.style.Theme_Red);
    }

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
}

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (id == R.id.action_settings) {
            Intent i = new Intent("com.cyanoise.helloworld.SettingsActivity");
            startActivity(i);

        return true;
    }

    if (id == R.id.about_app) {
        startActivity(new Intent(MainActivity.this, AboutActivity.class));

        return true;
    }

    if (id == R.id.refresh_app) {
        recreate();

        return true;
    }

    if (id == R.id.exitapp) {
        finish();
    }

    return super.onOptionsItemSelected(item);
}

SettingsActivity.java:

public class SettingsActivity extends PreferenceActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.settings);
    }
}

All answers are greatly appreciated.


Solution

  • Okay so I've been playing around and found a workaround to it. I added a onBackPressed() in the SettingsActivity to bring up the MainActivity again and it seemed to work, applying the selected theme once exiting the settings activity.

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        startActivity(new Intent(SettingsActivity.this, MainActivity.class));
    }
    

    As for applying themes to all activities i just added

    SharedPreferences getData = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        String themeValues = getData.getString("theme_preference", "1");
    
        if (themeValues.equals("1")) {
            setTheme(R.style.Theme_Light);
        }
    
        if (themeValues.equals("2")) {
            setTheme(R.style.Theme_Dark);
        }
    
        if (themeValues.equals("3")) {
            setTheme(R.style.Theme_Red);
        }
    

    to every onCreate of every activity.