Sebastien FERRAND
Sebastien FERRAND

Reputation: 2150

button dynamic background color with rounded edges

I managed to have rounded edge for my button.

I also managed to have a dynamic background color (taken from a webservice).

The problem is when doing this :

   btn.setBackgroundResource(R.drawable.radio_button_selector);
   btn.setBackgroundColor(Color.parseColor(currentQuestion.backgroundColorButton));

One overrides the other, therefore I cannot have rounded edges AND dynamic background color.

I cannot use a dynamic color in the selector (as it's a static XML).

I cannot set the rounded edges programmatically (the method doesn't exists as far as I know).

How do I do ?

Upvotes: 1

Views: 137

Answers (2)

Abhinav Gupta
Abhinav Gupta

Reputation: 2265

use this :-

final int color = Color.parseColor(homeCatPOJOS.get(position).getColor());

then implement it in background color :-

btn.setBackgroundResource(R.drawable.radio_button_selector);
btn.setBackgroundColor(color));

I think you are getting color code in String, first convert into int then implement it. And for round edges make XML file for it and implement it statically.

Upvotes: 0

Gowthaman M
Gowthaman M

Reputation: 8272

Use this

 String backgroundColor= "#fc0000"; // set dynamic color here

 btn.setBackgroundColor(Color.parseColor(backgroundColor));

another Examples:

GradientDrawable drawable = new GradientDrawable();
drawable.setShape(GradientDrawable.RECTANGLE);
drawable.setStroke(5, Color.MAGENTA); 
drawable.setColor(Color.BLACK); 
btnBlackColor.setBackgroundDrawable(drawable);

Upvotes: 1

Related Questions