TextInputLayout changing color programmatically - android

I would like to change the color style for the text input layout programmatically. The color is changing any moment so I need to set the color of the underline and hint text with the color I get from my server.
I could change the hint text color with the following code from another answer, but I can not change the underline color:
public static void setInputTextLayoutColor(TextInputLayout textInputLayout, #ColorInt int color) {
try {
Field fDefaultTextColor = TextInputLayout.class.getDeclaredField("mDefaultTextColor");
fDefaultTextColor.setAccessible(true);
fDefaultTextColor.set(textInputLayout, new ColorStateList(new int[][]{{0}}, new int[]{ color }));
Field fFocusedTextColor = TextInputLayout.class.getDeclaredField("mFocusedTextColor");
fFocusedTextColor.setAccessible(true);
fFocusedTextColor.set(textInputLayout, new ColorStateList(new int[][]{{0}}, new int[]{ color }));
} catch (Exception e) {
e.printStackTrace();
}
}

Try this
textInputLAyout.getEditText().getBackground().mutate().setColorFilter(your_color), PorterDuff.Mode.SRC_ATOP);

Related

Show Dynamic colors on recyclerview list numberings

I want to achieve something like this:
See the image here
A list with circular shape drawable with a fill of a light variant color of the image tint color
I have tried textrdrawablelibrary but it does not give me the same. I have tried the following code too but to no avail:
public int darkenColor(int color) {
float[] hsv = new float[3];
Color.colorToHSV(color, hsv);
hsv[2] *= 0.8f; // value component
color = Color.HSVToColor(hsv);
return color;
}
public String[] mColors = {
"5E97F6",
"9CCC65",
"FF8A65",
"9E9E9E",
"9FA8DA",
"90A4AE",
"AED581",
"F6BF26",
"FFA726",
"4DD0E1",
"BA68C8",
"A1887F",
};
// all colors used by gmail application :)
light colors
// genrating random num from 0 to 11 because you can add more or less
int i1 = new Random().nextInt(11);
//genrating shape with colors
GradientDrawable draw = new GradientDrawable();
draw.setShape(GradientDrawable.OVAL);
draw.setColor(Color.parseColor ("#"+mColors[i1]));
// assigning to textview
contact_name_circle.setBackground(draw); //textview

setStrokeColor not working programmatically

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)

Change the TextInputLayout outline box color programmatically

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))

drawable design 3 corners with one same color and else 1 corner with other color

Colour Code : e32b0f, d03559, I want to design its background drawable please help me
Try,
int colors[] = { 0xff255779, 0xfcc6c0cd };
GradientDrawable gradientDrawable = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM, colors);
view.setBackgroundDrawable(gradientDrawable);
1. choose color and add color hex code in colors array.
2. GradientDrawable using and set orientation properties.[ex. GradientDrawable.Orientation.TOP_BOTTOM]
3. set view background [ex. Linearlayout.setBackgroundDrawable(gradientDrawable);]

Get the background color of a button in android

How do i get the background color of a button.
In the xml i set the background color using ---- android:background = XXXXX
now in the activity class how can i retrieve this value that it has ?
Unfortunately I don't know how to retrieve the actual color.
It's easy to get this as a Drawable
Button button = (Button) findViewById(R.id.my_button);
Drawable buttonBackground = button.getBackground();
If you know this is a color then you can try
ColorDrawable buttonColor = (ColorDrawable) button.getBackground();
And if you're on Android 3.0+ you can get out the resource id of the color.
int colorId = buttonColor.getColor();
And compare this to your assigned colors, ie.
if (colorID == R.color.green) {
log("color is green");
}
private Bitmap mBitmap;
private Canvas mCanvas;
private Rect mBounds;
public void initIfNeeded() {
if(mBitmap == null) {
mBitmap = Bitmap.createBitmap(1,1, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mBounds = new Rect();
}
}
public int getBackgroundColor(View view) {
// The actual color, not the id.
int color = Color.BLACK;
if(view.getBackground() instanceof ColorDrawable) {
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
initIfNeeded();
// If the ColorDrawable makes use of its bounds in the draw method,
// we may not be able to get the color we want. This is not the usual
// case before Ice Cream Sandwich (4.0.1 r1).
// Yet, we change the bounds temporarily, just to be sure that we are
// successful.
ColorDrawable colorDrawable = (ColorDrawable)view.getBackground();
mBounds.set(colorDrawable.getBounds()); // Save the original bounds.
colorDrawable.setBounds(0, 0, 1, 1); // Change the bounds.
colorDrawable.draw(mCanvas);
color = mBitmap.getPixel(0, 0);
colorDrawable.setBounds(mBounds); // Restore the original bounds.
}
else {
color = ((ColorDrawable)view.getBackground()).getColor();
}
}
return color;
}
You can also try something like set the color value as the tag like
android:tag="#ff0000"
And access it from the code
String colorCode = (String)btn.getTag();
The simpliest way to get the color for me is:
int color = ((ColorDrawable)button.getBackground()).getColor();
Tested and working on Lollipop 5.1.1
To get the background Drawable, you use
public Drawable getBackground();
as defined in the base View class.
Don't forget that the Button can have a background that is an image, a color, a gradient. If you use android:background="#ffffff", the class of the background will be
android.graphics.drawable.ColorDrawable
From there you can simply call
public int getColor()
Try this:
list_view.getChildAt(position).setBackgroundColor(Color.YELLOW);
ColorDrawable corItem = (ColorDrawable) list_view.getChildAt(position).getBackground();
if(corItem.getColor() == Color.YELLOW){
Toast.makeText(NovoProcessoActivity.this,"Right Color!", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(NovoProcessoActivity.this,"Wrong Color!", Toast.LENGTH_SHORT).show();
}
or
int color =( (ColorDrawable) list_view.getChildAt(position).getBackground()).getColor();
int colornumber=((ColorDrawable)v.getBackground()).getColor();
This is the best and simple way to get the color of a View (Button, TextView...)
To get set color to a View using java we use
v.setBackgroundColor(getColor(R.color.colorname_you_used));

Categories

Resources