Change font of text next to radio button?
Asked Answered
R

5

21

To change the font of a textView I used

TextView tv = (TextView) findViewById(R.id.textview);
Typeface font = Typeface.createFromAsset(getAssets(), "SF_Cartoonist_Hand_Bold.ttf");
tv.setTypeface(font);

I want to do something similar for the text next to a Radio Button, how do I go about doing that?

Reredos answered 14/6, 2013 at 16:51 Comment(1)
Isn't it the same way? RadioButton has a setTypeface() method.Yearling
Y
29

You set the font to the text next to a RadioButton the same way you do to a TextView:

RadioButton rb  = (RadioButton) findViewById(R.id.radiobutton);
Typeface font = Typeface.createFromAsset(getAssets(), "SF_Cartoonist_Hand_Bold.ttf");
rb.setTypeface(font);
Yearling answered 14/6, 2013 at 16:58 Comment(0)
S
2

From the official doc:

Typeface typeface = getResources().getFont(R.font.myfont);
textView.setTypeface(typeface);
Smallage answered 18/4, 2018 at 15:47 Comment(1)
call required api 26. for less than 26 what about?Kirby
Z
2

I personally like to handle the styling outside of your fragment or activity. The following CustomRadioButton class should take care of it.

XML Layout:

<com.company.package.ui.CustomRadioButton
    android:id="@+id/radioBtnAge"
    style="@style/RadioButton.One"
    app:fontName="SF-Pro-Text-Medium.otf"/>

CustomRadioButton class:

package com.company.package.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;

public class CustomRadioButton extends android.support.v7.widget.AppCompatRadioButton {
    public CustomRadioButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs);
    }

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

    }

    public CustomRadioButton(Context context) {
        super(context);
        init(null);
    }

private void init(AttributeSet attrs) {
    if (attrs != null) {
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CustomTextView);
        String fontName = a.getString(R.styleable.CustomTextView_fontName);

        if (fontName == null || fontName.isEmpty()) {
            fontName = FontCacheHelper.DEFAULT_FONT_NAME;
        }

        Typeface myTypeface = FontCacheHelper.getInstance().getAppFont(getContext(), fontName);
        setTypeface(myTypeface);
        a.recycle();
    }
}
}

FontCacheHelper to manage fonts efficiently (reference: https://slothdevelopers.wordpress.com/2014/05/11/custom-fonts-in-textview-and-fontcache/)

package com.company.package.util;

import android.content.Context;
import android.graphics.Typeface;
import android.support.annotation.NonNull;

import java.util.HashMap;
import java.util.Map;

public class FontCacheHelper {
    private static final String FEATURE_FONT_PATH = "Fonts/";
    private static final String APP_FONT_PATH = "Fonts/App/";
    public static final String DEFAULT_FONT_NAME = "SF-Pro-Text-Medium.otf";
    private static FontCacheHelper fontCacheHelper;
    private Map<String, Typeface> fontMap = new HashMap<>();

    public static FontCacheHelper getInstance() {
        if (fontCacheHelper == null) {
            fontCacheHelper = new FontCacheHelper();
        }
        return fontCacheHelper;
    }

    public
    @NonNull
    Typeface getAppFont(@NonNull Context context, @NonNull String fontName) {
        String fontPath = getAppFontFilePath(fontName); 
        return getFontByPath(context, fontPath);
    }

    public
    @NonNull
    Typeface getFeatureFont(@NonNull Context context, @NonNull String fontName) {
        String fontPath = getFeatureFontFilePath(fontName);
        return getFontByPath(context, fontPath);
    }

    public
    @NonNull
    Typeface getFontByPath(@NonNull Context context, @NonNull String fontPath) {
        if (fontMap.containsKey(fontPath)) {
            return fontMap.get(fontPath);
        } else {
            Typeface typeface;
            if (fontPath.isEmpty()) {
                typeface = Typeface.DEFAULT;
            } else {
                typeface = Typeface.createFromAsset(context.getAssets(), fontPath);
                if (typeface == null) {
                    typeface = Typeface.DEFAULT;
                }
            }
            fontMap.put(fontPath, typeface);
            return typeface;
        }
    }

    public
    @NonNull
    String getDefaultFontFilePath() {
        return APP_FONT_PATH + DEFAULT_FONT_NAME;
    }

    private
    @NonNull
    String getAppFontFilePath(@NonNull String fontName) {
        return APP_FONT_PATH + fontName;
    }

    private
    @NonNull
    String getFeatureFontFilePath(@NonNull String fontName) {
        return FEATURE_FONT_PATH + fontName;
    }

    public
    @NonNull
    String getFontName(@NonNull Typeface typeface) {
        for (Map.Entry<String, Typeface> entry : fontMap.entrySet()) {
            if (entry.getValue().equals(typeface)) {
                return entry.getKey();
            }
        }
        return "";
    }
}
Zhang answered 3/5, 2018 at 20:14 Comment(0)
M
2

In kotlin, programmatically you can change the font of radio button

val typeface =ResourcesCompat.getFont(context, R.font.myfont) 
radioButton.typeface= typeface
Maidy answered 29/9, 2021 at 5:31 Comment(0)
F
0

As I described in this answer: https://mcmap.net/q/659967/-can-the-font-type-of-an-edittext-radio-button-and-checkbox-be-changed-in-android

you can use this line:

  app:fontFamily="@font/myFontFamily"
Forbis answered 12/12, 2020 at 6:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.