Search code examples
javaandroidandroid-theme

Switching themes


I want to add multiple themes in my app to allow users to change the app themes at runtime. I found code online which I have successfully added to my app, but I'm only able to change the theme of one activity which is not really my intention. When a user changes theme in the theme settings activity, I need that change to be applied in all activities.

ThemeActivity.java

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    themeUtils.onActivityCreateSetTheme(this);
    setContentView(R.layout.activity_theme);


    blackbtn = findViewById(R.id.blackbutton);
    bluebtn = findViewById(R.id.bluebutton);

    blackbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            themeUtils.changeToTheme(ThemeActivity.this, themeUtils.BLACK);
        }
    });

    bluebtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            themeUtils.changeToTheme(ThemeActivity.this, themeUtils.BLUE);
        }
    });

}

ThemeUtils.java

public class themeUtils
{

private static int cTheme;
public final static int BLACK = 0;
public final static int BLUE = 1;

public static void changeToTheme(Activity activity, int theme)
{
    cTheme = theme;
    activity.finish();
    activity.startActivity(new Intent(activity, activity.getClass()));
}

public static void onActivityCreateSetTheme(Activity activity)
{
    switch (cTheme)
    {

        default:
        case BLACK:
            activity.setTheme(R.style.BlackTheme);
            break;
        case BLUE:
            activity.setTheme(R.style.BlueTheme);
            break;
    }
}

Solution

  • use attribute value to set the color

    for exmaple here's the color for textview

    <attr name="textviewcolor" format="color"></attr>
    

    create differet styles for different theme selection in style.xml

    here's the textcolor for dark theme style

    <style name="AppTheme.Dark" parent="Theme.AppCompat.Light.NoActionBar"><item name="textviewcolor">#fff</item></style>
    

    here's the textcolor for defult theme style

    <style name="AppTheme.Defult" parent="Theme.AppCompat.Light.NoActionBar"><item name="textviewcolor">#000</item></style>
    

    use this attribute value(textviewcolor) to textview to set color like this

                          <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="text"
                            android:textColor="?attr/textviewcolor"/>
    

    you just have to change the theme name in sharedpreference in button click and refresh the activity rest of things are mentioned below how you gonna do it ..

    first create theme util class like this

    public class ThemeUtil {
    
    public static final int THEME_DEFAULT=1;
    public static final int THEME_DARK=2;
    public static final int ALERTTHEME=3;
    public static final int ALERTTHEMEDARK=4;
    
    
    
    public static int getThemeId(int theme){
        int themeId=0;
        switch (theme){
    
            case THEME_DARK:
                themeId = R.style.AppTheme_Dark;
                break;
    
            case THEME_DEFAULT :
                themeId = R.style.AppTheme;
                break;
    
            default:
                break;
        }
        return themeId;
    }}
    

    suggestion: use sharedpreference to give name of your theme

    then create one abstract class to set theme to all the activity using extending that class

    public class ChangethemeActivity extends AppCompatActivity{
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
    
        //get your theme name using sharedpreference and check what you have saved in theme name value
    
        if(dark){
        setTheme(ThemeUtil.getThemeId(1));
        }
        else{
          setTheme(ThemeUtil.getThemeId(2));
        }  }}}
    

    and finally: use ChangethemeActivity in activity instead of AppCompatActivity in your activity where you want to change the theme

    this is the way i've achieved theme change feature to app let me know if this solved your problem