You can change the font type of Edittext, Radio Button and CheckBox in Android

I am starting to work in android. I can change the font type of Textview in Android. But I need to use the .ttf file in the resource folder to make this kind of font change.

TextView text = (TextView) layout.findViewById(R.id.text);
text.setText(msg);
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/handsean.ttf");
text.setTypeface(font); 

The above code is what I used to change the font of the text of the View.but. I need to change the font type of the text of the Radio Button, Edittext, and Check buttons (which is also used in my application). Plz help me here. Thanks in advance.

+5
source share
4 answers

Yes, you must follow the same code that you mentioned here. This will work for other controls as well, such as Edittext, CheckBox, etc.

+6
source

, :

EditText

EditText editText = (EditText) layout.findViewById(R.id.edittext);
editText.setText(msg);
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/myfont.ttf");
editText.setTypeface(font);

RadioButton

RadioButton radioButton = (RadioButton) layout.findViewById(R.id.radiobutton);
radioButton.setText(msg);
Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/myfont.ttf");
radioButton.setTypeface(font);

CheckBox

CheckBox checkBox = (CheckBox) layout.findViewById(R.id.checkbox);
checkBox.setText(msg);
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/myfont.ttf");
checkBox.setTypeface(font);

, EditText, RadioButton CheckBox. . CheckBox.

public class MyCheckBox extends CheckBox {

    // Constructors
    public MyCheckBox(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }
    public MyCheckBox(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    public MyCheckBox(Context context) {
        super(context);
        init();
    }

    // This class requires myfont.ttf to be in the assets/fonts folder
    private void init() {
        Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
                "fonts/myfont.ttf");
        setTypeface(tf);
    }
}

xml :

<com.example.projectname.MyCheckBox
    android:id="@+id/checkbox"
    android:text="@string/msg"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:checked="true"/>
+3

Yes, the same approach will work for buttons and radio buttons.
You can also do this in the following simple steps.

Button moreBtn = (Button) events.findViewById(R.id.promotion_event_more_btn);
FontUtils.setTypeface(this, moreBtn, Constants.C_FONT);</code><br>Where "Constants.C_FONT" is set to the path of the font file present in the assets folder.
0
source

you can do this in the .xml file as well

you can use proertiees for this

    android:textSize="20dp"
    android:textStyle="bold"
    android:textColor="any color"
    android:textAppearance="any appearencde"

you can use them with buttons, checboxes, etc.

u can also execute code

0
source

All Articles