maltaisn / icondialoglib

Material icon picker dialog for Android
Apache License 2.0
120 stars 17 forks source link

Change style of dialog programmatically #31

Closed ghost closed 4 years ago

ghost commented 4 years ago

Hello, thank you for creating the iconpicker!

I have followed your post on styling the dialog through styles.xml, but how about changing style properties dynamically? For example, changing the "Cancel" and "Select" button colors, icon colors (not selected), and search icon color in code.

maltaisn commented 4 years ago

Sorry for the late response. I don't know if this is possible because I don't have much experience with changing styles programmatically. But I think since the dialog has no public API to be created with a custom theme or a custom style, this might not be possible.

Are you trying to do something like a setting in your app to let user choose the accent color? I think maybe if you change the calling activity or fragment theme, the dialog should use the theme values correctly, since internally it uses a ContextThemeWrapper on the context used to attach it.

ghost commented 4 years ago

Hello, maltaisn, thank you for the reply! Yes, you are correct, I would like to change certain colors of the icondialog dynamically when the user chooses for example a custom accent color. Hm, I think setting theme values in xml is not an issue, but could it be possible to add color getters and setters for the IconDialog.Builder, or even the main IconDialog?

For instance: IconDialogSettings.Builder icon_builder=new IconDialogSettings.Builder(); icon_builder.setIconColor(Color.RED); // <- suggestion IconDialogSettings iconSelector=icon_builder.build();

Could this be possible? int color = iconSelector.getIconColor(); iconSelector.setIconColor(Color.RED);

I have included your Icon Selector with gradle, but if I have time I could take a look at it and see if I can add some getters and setters for colors. However, I'm still learning about subclasses.

maltaisn commented 4 years ago

I'm not sure that this is the right solution. I'd prefer to leave theming to style attributes. You could do something like this instead:

Declare custom IcdStyles for the colors you need:

<style name="IconDialogStyleBlue" parent="IcdStyle">
    <item name="icdIconColor">@color/blue</item>
</style>
<style name="IconDialogStyleRed" parent="IcdStyle">
    <item name="icdIconColor">@color/red</item>
</style>

Create theme overlays for each icon dialog style:

<style name="ThemeOverlayIconDialogBlue">
    <item name="icdStyle">@style/IconDialogStyleBlue</item>
</style>
<style name="ThemeOverlayIconDialogRed">
    <item name="icdStyle">@style/IconDialogStyleRed</item>
</style>

Apply the theme overlay on the parent activity theme before showing the dialog:

theme.applyStyle(R.style.ThemeOverlayIconDialogRed, true)

This gets a bit long with many colors, but it has the advantage of being very flexible; you can do the same for all attributes without the need for a setter. However, if you want allow users to select any color with a color picker, then that won't work.

ghost commented 4 years ago

Hm, yes, thanks for the suggestion, but rather than using predetermined colors, I'd like to choose colors with, as you said, a color picker. I'm not actually changing theme colors or using multiple themes, I just change colors of everything that use the accent color, e.g. fabs, dialog button text, etc.

maltaisn commented 4 years ago

Hmm, so themes won't work. But I won't add a setter since there would be 2 ways of doing the same thing, and I feel like it's a hack. After all, the problem is more with android not letting you change theme attributes programmatically.

You can always fork and build your own version of the library, it shouldn't be too much work.

ghost commented 4 years ago

Sure, I'll try. Yes, I haven't found a native way to change theme colors programmatically. Should I make a pull request if I make a setter?

maltaisn commented 4 years ago

I did some research and it seems that changing theme colors programmatically is impossible. Thanks for the proposition, but don't make a pull request. I think it's too niche, most people just have 2-3 built in themes.