I've got this Activity in my app that allows the user to select colors from a color picker:
I save the custom colors in SharedPreferences. What I'm trying to achieve now is to set those colors to ActionBar, TextViews, Buttons and other widget.
I've searched like a lot and couldn't find a way to edit the Theme programmatically.
Any ideas on how to achieve this without, ofcourse, insanely set background colors to like every single widget in my app?
EDIT:
This is why I'm trying to do:
Let's say that if the user doesn't like the setup of the colors of the default app theme (Orange ActionBar, Orange headers, Black Text, Orange and Grey buttons, etc) he then would use this feature that allows him to change the theme MAIN COLORS. For instance, he would just like the buttons to be blue, or the EditText to have a yellow background. I'm trying to create a feature that would allow the user to edit the app's theme.
This is what I've tried:
I've found this library that changes the color of the EndScroll Highlight. I was thinking I could use these ideas to achieve the thing described above, by then creating base classes for the widgets I'm using in my app.
Yes, views background colors can be changed on the fly.
If view has the gradient applied than use:
GradientDrawable sd = (GradientDrawable) (view.getCompoundDrawables())[0];
sd.setColor(Color.red);
sd.invalidateSelf();
or for the LayerDrawable
LayerDrawable layer = (LayerDrawable)(view.getBackground());
int randomColor = Color.rgb(redRandom.nextInt(255),greenRandom.nextInt(255),blueRandom.nextInt(255));
GradientDrawable sd = (GradientDrawable) (layer.findDrawableByLayerId(R.id.tab_package_shape));
sd.setStroke(0, randomColor);
sd.setColor(randomColor);
sd.invalidateSelf();
Hope it will help you and provide the way to go further.
Related
I am trying to beautify some of the UI components in Android. I need to create a button with black border and background color. Everything is fine if I hardcode the variables into XML. But i have different buttons with different background color and different border color and size and etc. all the variations you could say. And it is surely wrong if I go and create all the variations in separate XML files. At the moment, I am creating styles in code but it looks a little inefficient.
gradient.setColor(getContext().getResources().getColor(R.color.ORANGE));
gradient.setCornerRadius(5);
gradient.setStroke(5, 0xFF000000);
button.setBackground(gradient);
And if I want to have button with different background color, I need to create another gradient and initialize it and set to button's resource. Another thing, when I am setting the background of the button, I am also losing the color change when the button is pressed. How do you create an XML drawable resource that can be customized in the interface? Or what is the easier way?
Thanks.
used this:-
Drawable mDrawable = ContextCompat.getDrawable(youContext, R.drawable.youdrawable)
mDrawable.setColorFilter(PorterDuffColorFilter(youColor, PorterDuff.Mode.MULTIPLY)
button.setbackground(mDrawable)
What I Want
I have has got a pre-defined color as the primary and accent color. But not all my users like the color I like. So, I want to create a color chooser with 10-15 predefined colors, which the user can choose to make as his/her primary and accent color.
I have read that the styles.xml file is immutable, so what is the way I can achieve this. I have seen many apps like Talon for Twitter Plus and many more doing this.
How can I do this? A working solution would be really appreciated.
EDIT - The user can select between Light theme and Dark theme. And there will be some preset colors which when selected will change the primary color and accent color of the theme, so that any widget in the app using these two colors get changed automatically.
Which parts of your app do you want to change?
Easiest way would be to write your colors in a color.xml file and simply change the background colors of your parts.
Other languages I used to use some constants that named something like "BackgroundColor" or "TextColor". If I want to color my component I just set its color using that constants, and I didn't care what color it was in particular.
For example, I set some color to ColorText and when user changed main color sceme my component will be visible for sure and color will suit this color sceme. And if I will set my color to just Black there may be a circumstance when it just will not be visible.
Is there a color-constants that change inside for different color scemes in android?
No, there's no one BackgroundColor for all etc. But you can get the same effect by using styles and themes. Please developer docs here and example tutorial here
I'm trying to reuse the same greyish gradient background inside the Android EditText? I want to use it as the background of color of a TextView? How do I specify this gradient for my TextView? I am not interested in Gradient for the Text itself just the background that it is written into like on an EditText?
This information is typically inside the specific Theme for your device. See the themes page on the Android website for more information about how to read and use them. Depending on your device, you could have a variety of different active themes so you will need to locate the source for the Theme you are looking for to acquire the exact gradient.
StateListDrawables and <selector> are great for setting different drawables for each state of a view, but is there a way to tie in what text style that view should use for each of those states?
For example, if you had a button that was white with black text, and when clicked, the button color became black, you would want your text color to change to white. I can imagine how I would do this with code, but is there a way to do it with XML similar to the <selector> used for drawables?
Well there sort of is. I don't believe there is a way to say, something like state_focused use Bold or italics or fontSize = 20. But there are ways to change the color. So using the selector mechanic you can create a ColorStateList
http://developer.android.com/reference/android/content/res/ColorStateList.html
Basically you do the same thing as you would a state list drawable using a selector except that you can place a color inside the individual items. Then you save your XML inside the color folder.
/res/values/color/my_stateful_color.xml
and set the android:textColor to "#color/my_stateful_color"