简体   繁体   中英

How to set themes dynamically?

I want to add themes to my application. For this I added color picker dialog.

When the user will select the color from color picker theme of the application should change, I want to change the color of navigation bar and floating button.

How can I set themes dynamically?

Thank you..

EDIT :

Settings Activity.

public class Settings extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        final ColorPickerDialog colorPickerDialog = new ColorPickerDialog();
        colorPickerDialog.initialize(R.string.dialog_title, new int[]{Color.CYAN, Color.LTGRAY, Color.BLACK, Color.BLUE, Color.GREEN, Color.MAGENTA, Color.RED, Color.GRAY, Color.YELLOW}, Color.YELLOW, 3, 2);
        colorPickerDialog.setOnColorSelectedListener(new ColorPickerSwatch.OnColorSelectedListener() {

            @Override
            public void onColorSelected(int color) {


                Toast.makeText(Settings.this, "selectedColor : " + color, Toast.LENGTH_SHORT).show();
            }
        });

        LinearLayout theme = (LinearLayout)findViewById(R.id.theme);

        theme.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                colorPickerDialog.show(getSupportFragmentManager(), "colorpicker");
            }
        });
    }

}

You can either use setTheme() in your activity and recreate the activity
OR
you can use setTheme() in your activity and set the colors manually (toolbar background, fab color, statusbar color) if you know exactly, which colors you want to change.

Note that setTheme() sets the theme dynamically, but it won't redraw your activity. So everything being redrawn after setTheme() already uses your new theme! (can be seen in Lollipop's/Marchmallow's recents apps list -> the color at the top of your app will already change after setTheme() even without recreating the activity).

It seems like there's no way to dynamically create a theme. So I'd recommend only offering a choice pre-defined themes (in XML) and applying these. A workaround for fully dynamic color changing would be to change the colors on all elements yourself. I wrote me a method to do this for toolbar and actionbar. So heres the method, based on the enum Colors {RED, GREEN, BLUE} :

private void setColors(COLORS color) {
    int toolbarColor = 0;
    int statusbarColor = 0;
    switch (color) {
        case GREEN:
            setTheme(R.style.AppTheme_NoActionBar_Green);
            toolbarColor = R.color.green;
            statusbarColor = R.color.greenDark;
            break;
        case RED:
            setTheme(R.style.AppTheme_NoActionBar_Red);
            toolbarColor = R.color.red;
            statusbarColor = R.color.redDark;
            break;
        case BLUE:
            setTheme(R.style.AppTheme_NoActionBar_Blue);
            toolbarColor = R.color.blue;
            statusbarColor = R.color.blueDark;
            break;
    }
    mToolbar.setBackgroundColor(ContextCompat.getColor(MainActivity.this, toolbarColor));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getWindow().setStatusBarColor(ContextCompat.getColor(MainActivity.this, statusbarColor));
    }
}

where AppTheme.NoActionBar.Green, AppTheme.NoActionBar.Red and AppTheme.NoActionBar.Blue extend AppTheme.NoActionBar and only overwrite the primary color, primary dark color and accent color.

You can't set theme dynamically. You can, however, set a theme before you call setContentView , so I suggest you store your theme in SharedPreferences , and when user selects new theme, restart activity and apply your new theme.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM