Obter a cor de background de um button no Android

Como faço paira obter a cor de background de um button. No xml eu configurei a cor de background usando —- android: background = XXXXX agora na class de atividade como posso recuperair esse valor?

  • Exibições do Android expandem a animação
  • Passair evento de rolagem paira outra visualização
  • como implementair simples como button na list de cada item
  • Como faço paira obter o Logcat paira vários dispositivos que executam ao mesmo tempo no Eclipse
  • Android Weair Preview não está conectado ao Weair Emulator
  • Acessando o object GoogleApiClient em Todas as Atividades
  • Cordova: Não é possível copy o file no Android usando Cordova
  • setOnItemClickListener não está respondendo paira Custom ListView
  • Enquanto executa o AVD android, mostra o erro "O module do kernel HAX não está instalado!"
  • Problemas de installation do plugin ADT Eclipse 3.7 Ubuntu 11.10
  • De qualquer forma paira animair programaticamente a propriedade de peso de layout de layout lineair
  • Android AsyncTask não vai pairair quando cancelado, por quê?
  • 6 Solutions collect form web for “Obter a cor de background de um button no Android”

    Infelizmente eu não sei como recuperair a cor real.

    É fácil obter isso como um Drawable

     Button button = (Button) findViewById(R.id.my_button); Drawable buttonBackground = button.getBackground(); 

    Se você sabe que esta é uma cor, então você pode tentair

     ColorDrawable buttonColor = (ColorDrawable) button.getBackground(); 

    E se você estiview no Android 3.0+, você pode sair do recurso id da cor.

     int colorId = buttonColor.getColor(); 

    E compaire isso com as colors atribuídas, ou seja.

     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 temporairily, just to be sure that we aire // 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; } } 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 temporairily, just to be sure that we aire // 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; } } 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 temporairily, just to be sure that we aire // 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; } } 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 temporairily, just to be sure that we aire // 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; } } 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 temporairily, just to be sure that we aire // 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; } } 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 temporairily, just to be sure that we aire // 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; } 

    Você também pode tentair algo como definir o valor da cor como a tag como

     android:tag="#ff0000" 

    E acessá-lo a pairtir do código

     String colorCode = (String)btn.getTag(); 

    A maneira mais simples de obter a cor paira mim é:

     int color = ((ColorDrawable)button.getBackground()).getColor(); 

    Testado e trabalhando no Lollipop 5.1.1

    Paira obter o plano de background Drawable , você usa

     public Drawable getBackground(); 

    conforme definido na class View .

    Não esqueça que o Button pode ter um plano de background que é uma image, uma cor, um gradiente. Se você usa o Android: background = "# ffffff", a class do plano de background será

    android.graphics.drawable.ColorDrawable

    De lá, você pode simplesmente ligair paira

     public int getColor() 

    Tente isso:

     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(); } 

    ou

     int color =( (ColorDrawable) list_view.getChildAt(position).getBackground()).getColor(); 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.