Because when comparing two Drawable resources the value is not the same?

0

Good morning, I'm trying to make the next comparison.
I have verified that both Backgrounds are the same, but still the comparison does not return a True.

if(contenedorMarcas.getBackground() == 
    getResources().getDrawable(R.drawable.contenedor_marcas, null)){
        datos.putBoolean("contenedorActivado", true);
}

Verifying the value of these by the AndroidStudio console I see these values, which are obviously different:

android.graphics.drawable.StateListDrawable@c749207
android.graphics.drawable.StateListDrawable@a8a7f34  

How can I make this comparison correctly?

    
asked by Parzival 21.06.2017 в 21:35
source

2 answers

1

The operator "==" compares references and in this case they are two different instances.

That you can check what is immediately after "@" which is the hash code of the object and as you see in your case are different.

Using .equals() is not enough either, in this answer where the user @vaughandroid raises an algorithm to compare equality in base to drawable.getConstantState() and Bitmap

    
answered by 21.06.2017 / 22:53
source
1

Try converting them to bitmaps. Since you are comparing the objects themselves and not their content.

Bitmap bitmap = ((BitmapDrawable)Drawable1).getBitmap();
Bitmap bitmap2 = ((BitmapDrawable)Drawable2).getBitmap();

if(bitmap == bitmap2){
  //Code blcok
}
    
answered by 21.06.2017 в 22:36