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 have extended Button to make a RachelButton and I set it up in the constructor.
public RachelButton(Context context, Info info) {
super(context);
this.info= info;
setText(info.getTime());
setTypeface(Typeface.DEFAULT, Typeface.BOLD);
int identifier = 0;
if(info.isAvailable()){
identifier = getContext().getResources().getIdentifier("drawable/info_button_"+info.getType(), null, getContext().getPackageName());
}else{
identifier = R.drawable.info_button_unavailable;
}
if(identifier == 0){
Log.e("INFO_BUTTON", "no button for "+info.getType());
}
setBackgroundResource(identifier);
setTextColor(R.color.info_button_text_color);
setOnClickListener(new View.OnClickListener(){
public void onClick(View view) {
//do stuff
}
});
}
Then an example of the resource I am using to generate a colored button is this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="@color/button_pressed"
android:endColor="@color/button_pressed"
android:angle="270" />
<stroke
android:width="3dp"
android:color="@color/button_pressed" />
<corners
android:radius="3dp" />
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp" />
</shape>
</item>
<item android:state_focused="true" >
<shape>
<gradient
android:endColor="@color/info_normal"
android:startColor="@color/info_normal"
android:angle="270" />
<stroke
android:width="3dp"
android:color="@color/info_normal" />
<corners
android:radius="3dp" />
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:endColor="@color/info_normal"
android:startColor="@color/info_normal"
android:angle="270" />
<stroke
android:width="3dp"
android:color="@color/info_normal" />
<corners
android:radius="3dp" />
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp" />
</shape>
</item>
</selector>
As you can see in the code I am setting the text color and I'm sure that this color exists as a resource (thank you IntelliJ).
But setting the text color like this has no effect at all, the text color on the button seems to be a darker shade of the button's background color.
If anyone could give me some advice as to what to try next I would be most appreciative.
To change the background color of the button, use the CSS background-color property and give it a value of a color of your taste. In the . button selector, you use background-color:#0a0a23; to change the background color of the button.
Show activity on this post. When I open a new android studio project, the default color for button is purple.
Type background-color: in the quotation marks after "style=". This element is used to change the background color of the button. Type a color name or hexadecimal code after "background-color:". You can type name of a color (i.e, blue) or a hexadecimal color.
Create background color. By default each activity in Android has a white background. To change the background color, first add a new color definition to the colors. xml file in the values resource folder like the following.
You should do:
setTextColor(getContext().getResources().getColor(R.color.info_button_text_color));
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With