I'm trying to set my color for the outline of my button, but I don't get it to work
I'm using material button and when I use
button.setStrokeColorResource(Color.parseColor(#e4dcd4))
is not working and tells me this
Expected a color resource id (R.color.) but received an RGB integer
I tried almost everything I could found about in stack, but I can't get it to set this strokeColor programmatically
Edit
Almost all setColors use #ColorInt , but this strokeColor uses #ColorRes, which is not working for me, also there is setStrokeColor
public void setStrokeColor(#Nullable ColorStateList strokeColor) {
if (isUsingOriginalBackground()) {
materialButtonHelper.setStrokeColor(strokeColor);
}
}
But I can't get it to work either.
It worked like this
val colorInt = Color.parseColor("#e4dcd4")
val csl = ColorStateList.valueOf(colorInt)
my_button.strokeColor = csl
You might try this
button.setStrokeColor(ContextCompat.getColor(this, R.color.your_color_xml));
Other way you can do is
ShapeDrawable gradientDrawable = (ShapeDrawable)button.getBackground();
gradientDrawable.setStroke(2, your_color);
Also as #Gabriele said you can get an int as a color as :
//From RGB
int colorRGB = Color.rgb(255,0,0);
//From HEX String
int colorHEX = Color.parseColor("#FF11AA");
You have to set the width of the stroke because the default value is 0.
<Button
app:strokeWidth="2dp"
../>
button.strokeColor = ColorStateList.valueOf(Color.parseColor("#e4dcd4"))
or
// if color define in color.xml
button.strokeColor = ColorStateList.valueOf(ContextCompat.getColor(this, R.color.yourColorCOde))
// if you have different state and you want to set programmatically then do as :-
var states = arrayOf(
intArrayOf(R.attr.state_enabled),
intArrayOf(-R.attr.state_enabled),
intArrayOf(-R.attr.state_checked),
intArrayOf(R.attr.state_pressed)
)
// Color list define respect of state
var colors = intArrayOf(
Color.BLACK,
Color.RED,
Color.GREEN,
Color.BLUE
)
// Set stroke color
button.strokeColor = ColorStateList(states, colors)
Related
I want to change 1 color value which will be represented by offset 1.0 to another color value represented by offset 0.0.
I don't want to use ValueAnimator because animation will be made by myself (function to change color is called everytime offset changes based on scroll listener) and I don't need to really "animate" it by duration.
I tried this:
val color = ArgbEvaluator().evaluate(offset, R.color.start, R.color.end)
But color is type of Any and not color I can set as backgroundTint for example.
You're on the right track, you're just missing a cast.
val color = ArgbEvaluator().evaluate(offset, startColor, endColor) as Int
myView.setBackgroundColor(color)
ArgbEvaluator#evaluate takes an
Object: A 32-bit int value representing colors in the separate bytes of the parameter
as its second and third arguments. What you are using are the resource id of the colors you want. You will need to translate those resource ids into 32-bit colors.
val startColor = ResourcesCompat.getColor(resources, R.color.start, null)
val endColor = ResourcesCompat.getColor(resources, R.color.end, null)
val color = ArgbEvaluator().evaluate(offset, startColor, endColor)
binding.textView.setBackgroundColor(color as Int)
I want to set background with gradient. This's my code:
val startColor = "0xFFAC235E"
val endColor = "0xFF640C35"
val gradient = GradientDrawable(
GradientDrawable.Orientation.LEFT_RIGHT,
intArrayOf(
startColor.toInt(),
endColor.toInt()
)
)
view.background = gradient
and it through an exception:
java.lang.NumberFormatException: For input string: "0xFFAC235E"
If I replace startColor = 0xFFAC235E, the code above work fine. But that's not what I want.
I need put color as param String. Is there anyway to convert it?
Try replacing 0x with #.
For ex:
startColor.replace("0x", "#")
Generally we define colors with hex color codes. So, I think this will work for you.
Edit
You have to parse the color string to convert it into integer.
Color.parseColor(startColor.replace("0x", "#"))
I would like to change outline of the TextInputLayout programmatically, but I cannot seem to get it to work. There is an option to do it via XML (question by other SO user using XML), but that is unusable for me as I need to have dynamic coloring. I currently have the following layout:
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:id="#+id/color_outline"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/color"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Choose color"/>
</com.google.android.material.textfield.TextInputLayout>
I've attempted to apply coloring by looking at the various box methods of the TextInputLayout, but it did not have any effect.
internal fun String.toIntColor() = Integer.parseInt(this.replaceFirst("#", ""), 16)
val colorOutline: TextInputLayout = view.findViewById(R.id.color_outline)
colorOutline.boxStrokeColor = "#006699".toIntColor()
How can I color it dynamically, like in the picture below?
Current situation:
Desired situation: (photoshopped)
Similar question, but focussing on XML
You can the method setBoxStrokeColorStateList.
Something like:
//Color from rgb
int color = Color.rgb(255,0,0);
//Color from hex string
int color2 = Color.parseColor("#FF11AA");
int[][] states = new int[][] {
new int[] { android.R.attr.state_focused}, // focused
new int[] { android.R.attr.state_hovered}, // hovered
new int[] { android.R.attr.state_enabled}, // enabled
new int[] { } //
};
int[] colors = new int[] {
color,
color,
color,
color2
};
ColorStateList myColorList = new ColorStateList(states, colors);
textInputLayout.setBoxStrokeColorStateList(myColorList);
In Kotlin
I have modified the #Gabriele's answer to make it working for me
You can define an extension function as :
private fun TextInputLayout.setBoxStrokeColorSelector() {
//Color from rgb
int color = Color.rgb(255,0,0);
//Color from hex string
val defaultColor = ContextCompat.getColor(context,R.color.indicator_def)
val states = arrayOf {
intArrayOf(android.R.attr.state_focused), // focused
// intArrayOf(android.R.attr.state_hovered), // hovered
intArrayOf(android.R.attr.state_enabled), // enabled
intArrayOf() // default
}
val colors = intArrayOf(color, // focused color
/*color,*/ // hovered color
color, // enabled color
defaultColor) // default color
val myColorList = ColorStateList(states, colors)
setBoxStrokeColorStateList(myColorList)
}
and just call it for any TextInputLayout in your app like
TextInputLayout.setBoxStrokeColorSelector(ContextCompat.getColor(this, R.color.colorPrimary))
I would like to make a graph like this :
The problem is I don't know how to set a gradient color like this using MPAndroidChart. Maybe I should use an other library ?
Maybe it's better to use progressbar with transparent color (and gradient background) ?
This is my code :
val entries = ArrayList<BarEntry>()
entries.add(BarEntry(1.toFloat(), 43.toFloat()))
entries.add(BarEntry(2.toFloat(), 3.toFloat()))
entries.add(BarEntry(3.toFloat(), 13.toFloat()))
entries.add(BarEntry(4.toFloat(), 41.toFloat()))
entries.add(BarEntry(5.toFloat(), 22.toFloat()))
entries.add(BarEntry(6.toFloat(), 11.toFloat()))
entries.add(BarEntry(7.toFloat(), 13.toFloat()))
entries.add(BarEntry(8.toFloat(), 99.toFloat()))
entries.add(BarEntry(9.toFloat(), 67.toFloat()))
entries.add(BarEntry(10.toFloat(), 3.toFloat()))
entries.add(BarEntry(11.toFloat(), 56.toFloat()))
entries.add(BarEntry(12.toFloat(), 88.toFloat()))
val dataSet = BarDataSet(entries, "Label")
chart.data = BarData(dataSet)
This worked for me:
dataset.setGradientColor(Color.parseColor("#00FF5722"),Color.parseColor("#FFFF5722"));
You can use setGradientFill() method which will accept n number of color.
int[] colors = { getResources().getColor(R.color.menu_text),
getResources().getColor(android.R.color.white) };
float[] index = { 0, 1 };
dataset.setGradientFill(colors, index);
for more you can refer this. Hope it will help you!!
I am currently working on an android APP and I have a list of predefined color in my colors.xml.
Now what I want to achieve is that I want to give the user the opportunity of selecting a color and from the color the user select, I want to display various color (randomly) in the background that matches the global color the user selects, such that those colors will be changing at interval. These colors that will be displayed randomly are already defined in my colors.xml file
Any Guidance or a lead sample on this requirement will be invaluable.
Thank you in advance
private int[] colors = {Color.RED, Color.YELLOW, Color.BLUE, Color.CYAN, Color.GREEN};
Random ranndom = new Random();
int ranndomColor = ranndom.nextInt(5);
whatever.setBackground(colors[ranndomColor]);
or get the colors from "colors.xml:
int color1 = getResources().getColor(R.color.color1);
int color2 = getResources().getColor(R.color.color2);
int color3 = getResources().getColor(R.color.color3);
int color4 = getResources().getColor(R.color.color4);
int color5 = getResources().getColor(R.color.color5);
private int[] colors = {color1, color2, color3, color4, color5};
Random ranndom = new Random();
int ranndomColor = ranndom.nextInt(5);
whatever.setBackground(colors[ranndomColor]);