Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can the font type of an Edittext,Radio Button and CheckBox be changed in Android

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.

like image 529
user1010764 Avatar asked Feb 22 '12 07:02

user1010764


2 Answers

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"/>
like image 161
Suragch Avatar answered Nov 14 '22 22:11

Suragch


Yes you have to follow the same code wht u have mentioned here.This will work for other controls too like Edittext,CheckBox etc.

like image 38
kaushik Avatar answered Nov 14 '22 23:11

kaushik