开发者

Android Help with Changing Button Font Type, How?

i would like to change the font that appears on the button text, i have managed to do this with text on the screen, textview, but cannot find any info, or help with applying this to a button.

I 'am novice, so providing the code to do so, would be much appreciated. This is what i'am using for the textview, but how do i change the button font?

TextView txt = (TextView) findViewById(开发者_开发问答R.id.custom_font);  
Typeface font = Typeface.createFromAsset(getAssets(), "1543Humane_jenson_bold.TTF");  
txt.setTypeface(font);

Thanks Lucy x


Button IS-A TextView, so just do it as with a TextView:

Button txt = (Button) findViewById(R.id.custom_font);  
Typeface font = Typeface.createFromAsset(getAssets(), "1543Humane_jenson_bold.TTF");  
txt.setTypeface(font);


I use like this for button and it worked (same as for TextView) ..

 Button enter=(Button) findViewById(R.id.enter);
 Typeface type=Typeface.createFromAsset(getAssets(), "arial.ttf");
 enter.setTypeface(type);

hope it helps...


If you plan to add the same font to several buttons I suggest that you go all the way and implement it as a style and subclass button:

public class ButtonPlus extends Button {

    public ButtonPlus(Context context) {
        super(context);
    }

    public ButtonPlus(Context context, AttributeSet attrs) {
        super(context, attrs);
        CustomFontHelper.setCustomFont(this, context, attrs);
    }

    public ButtonPlus(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        CustomFontHelper.setCustomFont(this, context, attrs);
    }
}

This is a helper class to set a font on a TextView (remember, Button is a subclass of TextView) based on the com.my.package:font attribute:

public class CustomFontHelper {

    /**
     * Sets a font on a textview based on the custom com.my.package:font attribute
     * If the custom font attribute isn't found in the attributes nothing happens
     * @param textview
     * @param context
     * @param attrs
     */
    public static void setCustomFont(TextView textview, Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomFont);
        String font = a.getString(R.styleable.CustomFont_font);
        setCustomFont(textview, font, context);
        a.recycle();
    }

    /**
     * Sets a font on a textview
     * @param textview
     * @param font
     * @param context
     */
    public static void setCustomFont(TextView textview, String font, Context context) {
        if(font == null) {
            return;
        }
        Typeface tf = FontCache.get(font, context);
        if(tf != null) {
            textview.setTypeface(tf);
        }
    }
 }

And here's the FontCache to reduce memory usage on older devices:

public class FontCache {

    private static Hashtable<String, Typeface> fontCache = new Hashtable<String, Typeface>();

    public static Typeface get(String name, Context context) {
        Typeface tf = fontCache.get(name);
        if(tf == null) {
            try {
                tf = Typeface.createFromAsset(context.getAssets(), name);
            }
            catch (Exception e) {
                return null;
            }
            fontCache.put(name, tf);
        }
        return tf;
    }
}

In res/values/attrs.xml we define the custom styleable attribute

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomFont">
        <attr name="font" format="string"/>
    </declare-styleable>
</resources>

And finally an example use in a layout:

<com.my.package.buttons.ButtonPlus
    style="@style/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_sometext"/>

And in res/values/style.xml

<style name="button" parent="@android:style/Widget.Button">
    <item name="com.my.package:font">fonts/copperplate_gothic_light.TTF</item>
</style>

This may seem like an awful lot of work, but you'll thank me once you have couple of handfuls of buttons and textfields that you want to change font on.


An other alternative for the above solutions:

mYourButton = (Button) findViewById(R.id.Button_id);
mYourEditText = (EditText) findViewById(R.id.editText_id);

Typeface font = ResourcesCompat.getFont(getContext(), R.font.font_name.TTF);
mYourButton.setTypeface(font);
mYourEditText.setTypeface(font);


By creating a custom button as shown here:

  public class Button_Roboto_Regular extends Button {

    public Button_Roboto_Regular(Context context) {
        super(context);
        mTextFont(context);
    }

    public Button_Roboto_Regular(Context context, AttributeSet attrs) {
        super(context, attrs);
        mTextFont(context);
    }

    public Button_Roboto_Regular(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mTextFont(context);
    }
    private void mTextFont(Context context) {
        Typeface face = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Regular_0.ttf");
        this.setTypeface(face);
    }
}


Steps: 1 - Create new Android Resource Directory. 2 - Select type as font(note, you try typing font in type selector u will find. ) 3 - keep name as font. 4 - create.

5 - take your TTF file (can download from internet) 6- paste it in font folder of android 7 - go to where ever you want to use font, and put:

android:fontfamily="@font/yourfont.ttf"

8 - if you wanna do it in whole project, which is very convinient for a non-default-font project, go to themes.xml and write this line in your theme:

<item name="android:fontFamily">@font/segoe_ui</item>

However, if you want to do it programmatically then answer by @Konstatin Burov is your best bet.

By XML, I have already informed you . Note: for those who want to know how this works, a) for theme, this sets setting for your whole project, and ttf file IS compatible with android. b) for specific usage, all attrs(not just buttons!, but any place U need to display text) it works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜