How to change text style based on state with XML? - android

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"

Related

Android Button with border and background color

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)

Change theme colors programmatically

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.

Default android.R.atrr. for ListView item background color

I know I can use the android.R.attr. for default values (like colors, styles, etc.)
Does anyone know which android.R.atrr. I need to use for the default ListView Item background color?
When I look at the available android.R.atrr. I see there is colorFocusedHighlight, colorLongPressedHighlight, colorMultiSelectHighlight and colorPressedHighlight, but not one for just the item that isn't pressed/focussed/etc.
It's actually a drawable, not a color like I mentioned in my comment.
http://developer.android.com/reference/android/R.drawable.html#list_selector_background

Change background color to tabhost

I would like to change the background of the tabwidget in shown in the next image:
I would like to use a different background color, and change the font color to the selected tab. How can I do that?
To do this i used drawables for tab widgets instead of fixed images. my code is
tab1.setImageDrawable(getResources().getDrawable(R.drawable.tab_drawable));
in the tab_drawable is a simple selector defining the behavior of the drawable for different state. you can try that it is very useful.
you can google for the tables available for different colors
this is a chart or table available with color and the code to get that
color
just choose which ever you like

ANDROID : Changing the color of ExpandableListView's group indicator

The problem : I've changed my app's background color to white, and now I cannot see the ExpandableListView's group indicator anymore.
ExpandableListView has methods to change text color, background color, and divider color, but nothing I can see for the group indicator color.
I do not want to provide custom drawables, I just want to change the color of the drawables that are provided by Android, programmatically.
I feel there must be some simple way to do this.
You will have to use custom drawables here. You can't change the android drawables provided by Android.

Categories

Resources