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

5

6

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.

Waterfront answered 22/2, 2012 at 7:4 Comment(2)
I guess, you have to use the same code for the other views too. Have you tried using the code to EditText etc.??Systematize
Yes you have to use the same code for edittext too.Axial
S
6

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

Santiago answered 22/2, 2012 at 7:12 Comment(1)
what about without assets folder? (with font folder that in new in android)Garrygarson
R
7

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"/>
Rothman answered 17/11, 2016 at 1:20 Comment(0)
S
6

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

Santiago answered 22/2, 2012 at 7:12 Comment(1)
what about without assets folder? (with font folder that in new in android)Garrygarson
W
0

Yes the same approach will work for buttons and radio buttons too.
Also you can do this in simple steps as follows

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.
Witkin answered 22/2, 2012 at 7:13 Comment(0)
L
0

you can do it so in .xml file as well

you can use proertiees to do so

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

you can use these with buttons , checboxes etc.

u can do the by code also

Legwork answered 22/2, 2012 at 7:13 Comment(0)
R
0

Make sure your support library is >=26.0 or using AndroidX in your project. Then add this line to your view in XML

      app:fontFamily="@font/myFontFamily"

e.g:

  <RadioButton
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/language_english"
      android:text="@string/english"
      app:fontFamily="@font/myFontFamily"
      />

If you want to use your custom font, read the official document: https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml

Restaurateur answered 12/12, 2020 at 6:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.