Question

I am using a custom button class to use a custom font for all the buttons in my app. I would like to change the color too but I don't know how. It is probably pretty easy but I couldn't figure it out.

Here is the code I use:

    public class CustomButton extends Button {

    public CustomButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public CustomButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CustomButton(Context context) {
        super(context);
        init();
    }

    private void init() {
        if (!isInEditMode()) {
            Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "MyCustomFont.otf");
            setTypeface(tf);
        }
    }
}
Was it helpful?

Solution 2

Try use setTextColor(Color.BLUE);

private void init() {
        if (!isInEditMode()) {
            Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "MyCustomFont.otf");
            setTypeface(tf);
            setTextColor(Color.BLUE);
        }
    }

OTHER TIPS

In manifest

<application android:theme="@style/ApplicationStyle" android:icon="@drawable/icon" android:label="@string/app_name"> 

In Mystyle.xml

<style name="ApplicationStyle" parent="android:Theme">
  <item name="android:buttonStyle">@style/CKButton</item>
</style>
 <style name="CKButton" parent="android:style/Widget.Button">
  <item name="android:textSize">19sp</item>
  <item name="android:layout_margin">0dip</item>
  <item name="android:background">#ff0000</item>
 </style>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top