The selected answer was missing the code, so here it is:
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);
Multiple Views
If you need to do this for multiple views across your application, then it may be easier to make a subclass of your EditText
, RadioButton
, or CheckBox
. This subclass sets the font. Below is an example for 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);
}
}
It can be used in xml as follows:
<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"/>