I am a beginner in android.I can able to change the font type of a Textview in Android.But I have to use .ttf file in asset folder,to bring 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 a text View.but I need to change the font type of the text of Radio Button,Edittext and Check box(which Im also used in my application) as well.Plz help me out here.Thanks in advance.
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);
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"/>
Yes you have to follow the same code wht u have mentioned here.This will work for other controls too like Edittext,CheckBox etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With