Android - How To Programmatically Set Button Color
I am reading in some data from a REST api and need to generate some buttons based on the information the app receives. Because I need the same buttons in many Activity screens I ha
Solution 1:
You should do:
setTextColor(getContext().getResources().getColor(R.color.info_button_text_color));
Solution 2:
Better if you have the View object (findViewById
from R class) transformed info specific object: for example Button. ( the standard way - Button b = (Button) fin...(R.id.sdfsdf)
)
Next just type from a few andro-colors:
b.setTextColor(Color.parseColor("green"));
or BETTER: FROM RGB
b.setTextColor(Color.rgb(0xff, 0x66, 0x33));
Everything is in the ctrl+spaceBar
in Eclipse :P
Sorry! Maybe the b.setTextColor(0xff0000)
would also works...
Solution 3:
The getColor() function is deprecated from API level 23. Check this link for more details. Below is the source code from the support library:
publicstaticfinalintgetColor(Context context, int id){
finalint version = Build.VERSION.SDK_INT;
if (version >= 23) {
return ContextCompatApi23.getColor(context, id);
} else {
return context.getResources().getColor(id);
}
}
Post a Comment for "Android - How To Programmatically Set Button Color"