Compare TextView color in android studio

0

How can I compare the color of a TextView with some color in my colors.xml

Something like this comparison of background but with the color of the text

if ( relajado.background.constantState == ContextCompat.getDrawable(ctx,R.drawable.bg_rectangle_no_selected)!!.constantState)
    
asked by César Alejandro M 30.09.2018 в 21:43
source

2 answers

1

First you have to get the color in decimal format with a formula:

For the TextView color:

 int colorText = textView.getCurrentTextColor();
 int colorDecimalText = (colorText)+(16777215+1);

To obtain the decimal of some color that is in resources (colors.xml):

 int colorRecurso = getResources().getColor(R.color.tuColor);
 int colorDecimalRecurso = (colorRecurso)+(16777215+1);

In this way you get the color in decimal that you can convert to Hex on websites like: link Hex is the color format in colors.xml. The formula + (16777215 + 1) is used because takes the white color base, which is subtracted from the first int.

Or use this code to convert it to Hex:

String hexColorText = "#" + Integer.toHexString(colorText).substring(2);
String hexColorRec = "#" + Integer.toHexString(colorRecurso).substring(2);

To compare in decimal :

 if(colorDecimalText == colorDecimalRecurso){
      ... tu código
    }

To compare as String (Hex):

  if(hexColorText.equals(hexColorRec)){
     ... tu código   
    }
    
answered by 30.09.2018 / 23:07
source
-1

If you want to compare the background of the textView it would be like this.

if (textView.getBackground() instanceof ColorDrawable) {
   ColorDrawable cd = (ColorDrawable) textView.getBackground();
   if(cd.getColor()==(int)Color.GREEN){
      //Tu logica
   }
}

If you want to compare the text color of the textView it would be like this.

if(textView.getCurrentTextColor()==(int)Color.GREEN){  //textView es tu textView 
   //Tu logica
}

Greetings.

    
answered by 30.09.2018 в 22:59