0

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);
        }
    }
}
Engin Yapici
  • 5,673
  • 5
  • 22
  • 32

2 Answers2

3

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>
Chirag Patel
  • 11,416
  • 3
  • 26
  • 38
1

Try use setTextColor(Color.BLUE);

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