Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

490
Visualizações
Android/Java: how to set background color of MaterialShapeDrawable with int Color?

I have a TextView with rounded corners.

Here is my code:

float radius = 10f;
ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel()
   .toBuilder()
   .setAllCorners(CornerFamily.ROUNDED,radius)
   .build();
    
MaterialShapeDrawable shapeDrawable = new MaterialShapeDrawable(shapeAppearanceModel);
    
ViewCompat.setBackground(textView,shapeDrawable);

Now, I want to change programmatically the background color of the textView.

When I do :

shapeDrawable.setFillColor(ContextCompat.getColorStateList(this,R.color.design_default_color_background));

it works; the background color is changed.

Now, I want to change the background color with an int color like Color.RED or any random color defined with Color.RGB(r, g, b, a) or Color.RGB(r, g, b).

How can I do that? Should I use shapeDrawable.setFillColor or another method?

Thanks.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

ANSWER to my question.

Here is the code I added in order to be able to set any background color :

 int[][] states = new int[][] {
                new int[] { android.R.attr.state_hovered}, // hovered
        };

 int[] colors = new int[] {color};
 ColorStateList myColorList = new ColorStateList(states, colors);
 shapeDrawable.setFillColor(myColorList);
 shapeDrawable.setState(states[0]);

Crazy to have to write so much code just to change a background color...

Thanks all for your help !

over 4 years ago · Santiago Trujillo Relatório

0

You can define global value for your color just like this

 public static final int RED = 0xffff0000;

And use like this.

 shapeDrawable.setFillColor(ContextCompat....(this, RED));
over 4 years ago · Santiago Trujillo Relatório

0

The method setFillColor works with a ColorStateList.
You can use something like:

int[][] states = new int[][] {
    new int[] { android.R.attr.state_focused}, // focused
    new int[] { android.R.attr.state_hovered}, // hovered
    new int[] { android.R.attr.state_enabled}, // enabled
    new int[] { }  // 
};

int[] colors = new int[] {
    Color.BLACK,
    Color.RED,
    Color....,
    Color....
};

ColorStateList myColorList = new ColorStateList(states, colors);
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda