Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

488
Views
Android/Java: ¿cómo establecer el color de fondo de MaterialShapeDrawable con int Color?

Tengo un TextView con esquinas redondeadas.

Aquí está mi código:

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

Ahora, quiero cambiar mediante programación el color de fondo de textView.

Cuando lo hago :

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

funciona; se cambia el color de fondo.

Ahora, quiero cambiar el color de fondo con un color int como Color.RED o cualquier color aleatorio definido con Color.RGB(r, g, b, a) o Color.RGB(r, g, b).

¿Cómo puedo hacer eso? ¿Debo usar shapeDrawable.setFillColor u otro método?

Gracias.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

RESPUESTA a mi pregunta.

Aquí está el código que agregué para poder establecer cualquier color de fondo:

 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]);

Es una locura tener que escribir tanto código solo para cambiar un color de fondo...

Gracias a todos por su ayuda !

over 4 years ago · Santiago Trujillo Report

0

Puede definir el valor global para su color así

 public static final int RED = 0xffff0000;

Y usar así.

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

0

El método setFillColor funciona con ColorStateList .
Puedes usar algo como:

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!