I am developing an Android App where I have a few EditTexts. I am setting the background color of the EditText dynamically with the one I have defined in the res/drawable folder. I want to get the background color of the EditText and compare it with some hex color, e-g if the color is #ff0000 then change the text color of that EditText to white. This is how I am setting the background color of the EditText:
allScaleEditTexts.get(row_col).setBackgroundResource
(R.drawable.edittext_bgcolor_one);
That is how I am getting the background color of the EditText:
Drawable scaleEdTxtColor = allScaleEditTexts.get(row_col)
.getBackground();
Now I want to make that comparison like:
if(scaleEdTxtColor == #ff0000)
allScaleEditTexts.get(row_col).setTextColor(Color.WHITE);
else
allScaleEditTexts.get(row_col).setTextColor(Color.BLACK);
I don't know how to go about that comparison. I have tried this:
if(scaleEdTxtColor == Color.parseColor("#FF0000"))
But then I get the error of comparing a drawable with an int. How can I solve it?
You can't just use
if(scaleEdTxtColor == #ff0000)
Cause its a drawable.
I think you need to scaledEdTxtColor.getColorFilter like you set the color with:
scaledEdTxtColor.setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);
Have a look there.
Related
I have a question about Set Background Color for a View programmatically.
I have integer value that I would like to use for background color for TextView. Now I have the Integer values of
-8076976
Is this possible to use this value to convert for be able to use background color?
Thank you very much
Edit
The integer value can be changed because of I used a seek bar that for picking color so that I would like to know convert the integer value and directly set as background color.
In your Color.xml file create a color item with this color followed by a #.
Then in your code use:
getResources.getColor(R.color.color-code);
Update:
As per your comment you want to show directly the hex value.
Look at the code here:
SetBackgroundColor(Color.parseColor("#000000"));
Replace with your own hex color code.
I want to change the color of my drawable/vector file programmatically at runtime, the color code will be coming from API, anyone know how i can change the colors at run time or is there any other way?
I want to change the color of my drawable/vector file programmatically at runtime
You can fill the tint color of your drawable at runtime using this:
newDrawable = yourDrawable.mutate();
newDrawable = DrawableCompat.wrap(newDrawable);
DrawableCompat.setTint(newDrawable, intColor);
DrawableCompat.setTintMode(newDrawable, PorterDuff.Mode.SRC_IN);
I have to create the background color for edittext means am getting the background correctly.
TestPost.this.findViewById(R.id.revieew)
.setBackgroundColor(color);
But how can i get the textcolor for these edittext .
TestPost.this.findViewById(R.id.revieew)
.setTextColor(color);
Please give me a solution ???
i have to pick the green color means have to enter the text is green....here i have done these for background color.i have to set the background color is green means have to pick the color green from color picker means the bexkground is successfully displaying...how can i set the textcolor from color picker ???
EDIT:
reviewEdit.setTextColor(color);
means am getting the color is successfully...But i didn't change the whole text.
I want to change the color for selected text alone...
For EG:
The text is : The customer is waiting for your reply means
Have to pick a color green, have to write the The customer is waiting have to display green color for these text alone.after that have to pick a color pink means have to display the for your reply as pink color.
This is exactly i need ...how can i implement these ???
((TextView)TestPost.this.findViewById(R.id.revieew)).setTextColor(color);
See the docs here
What you are doing is almost correct. The method findViewById() returns a View which you need to cast into a TextView/EditText (depending on how you have the view with that id defined in your xml) and then the method will be available for use.
EditText text = (EditText) findViewById(R.id.revieew);
text.setTextColor(color);
Try this TestPost.this.findViewById(R.id.revieew).getCurrentTextColor();
Found here: https://stackoverflow.com/a/6746131/2065418
Your method was correct for getting the color use below code
int color =editText.getCurrentTextColor();
Log.d("color", String.valueOf(color));
i would like to have tha default button but not in gray.
I need to change the color green/blue/red.
I would like to do this thanks to xml but also from the code.
I need to have a green button which change the color of all the other button.
It's why i need to change from xml and from the code.
Define you colors in the color resource so it is easier to apply your desired colors via code or xml.
I am trying to change the text color of a RadioButton (which is
defined in an xml layout and is in a RadioGroup) on selecting it.
When I change the text color directly in the Eclipse Android Layout
Editor by setting the TextColor property to "#color/red" (which I
defined in strings.xml), it works just fine, but when I try to do this
programmatically during runtime as
myRadioButton.setTextColor(R.color.red);
it only turns the color to grey, not to red as intended.
R.color.red (#color/red) is correctly defined as a hex value
("#FF0000"), but it does turn the text color to red in the
layout editor, but not via a Java command.
if your color.xml is like:
<color name="errorColor">#f00</color>
and then use this code to show it:
myRadioButton.setTextColor(getResources().getColor(R.color.red));
there are some other ways to do so
myRadioButton.setTextColor(Color.RED);
or
myRadioButton.setTextColor(Color.rgb(red, green, blue));
// where red green and blue are the int values
edited if you want to get from resources then use
getResources().getColor(R.color.red) ;