texto studio redondo pulsarlo personalizar imagen efectos diseño con color cambiar botón botones boton android button background-color

redondo - efectos botones android studio



Obtener el color de fondo de un botón en Android (6)

¿Cómo obtengo el color de fondo de un botón? En el xml establezco el color de fondo usando ---- android: background = XXXXX ahora en la clase de actividad ¿cómo puedo recuperar este valor que tiene?


La forma más sencilla de obtener el color para mí es:

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

Probado y trabajando en Lollipop 5.1.1


Lamentablemente, no sé cómo recuperar el color real.

Es fácil obtener esto como un Drawable

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

Si sabes que este es un color, puedes intentar

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

Y si tienes Android 3.0 o superior, puedes obtener la identificación del recurso del color.

int colorId = buttonColor.getColor();

Y compare esto con sus colores asignados, es decir.

if (colorID == R.color.green) { log("color is green"); }


Para obtener el fondo Drawable , usa

public Drawable getBackground();

como se define en la clase de View base.

No olvide que el Button puede tener un fondo que sea una imagen, un color o un degradado. Si usas android: background = "# ffffff", la clase del fondo será

android.graphics.drawable.ColorDrawable

Desde allí, simplemente puede llamar

public int getColor()


Prueba esto:

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

o

int color =( (ColorDrawable) list_view.getChildAt(position).getBackground()).getColor();


También puedes probar algo como establecer el valor de color como la etiqueta como

android:tag="#ff0000"

Y acceda desde el código

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


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