Issue
I am new in android app development. I want to change fonts of my Application. Just i wanna know different methods for change the font by XML and Java to. Want to change the font of : 1.TextView 2.RadioButton 3.EditText 4.CheckBox etc.
Solution
- In Android Studio Right click on app & create a folder assets.
Right click on assets and create a folder fonts.
Download .ttf file i.e fontName.ttf and paste inside fonts folder.
Now you have to do main things. Inside your package create a class.
This class is for TextView
public class Railway_Regular extends TextView {
public Railway_Regular(Context context, AttributeSet attrs) {
super(context, attrs);
this.setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/Raleway-Regular.ttf"));
}}
This class is for Button
public class Railway_Regular_Btn extends Button {
public Railway_Regular_Btn(Context context, AttributeSet attrs) {
super(context, attrs);
this.setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/Raleway-Regular.ttf"));
}}
This class is for EditText
public class Railway_Regular_EdTx extends EditText {
public Railway_Regular_EdTx(Context context, AttributeSet attrs) {
super(context, attrs);
this.setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/Raleway-Regular.ttf"));
}}
Like these you can create classes for all you Widgets & reference the fontname.ttf to your classes.
Now,for set textview as your font do this.
for TextView
<package_name.fonts.Railway_Regular
android:padding="5sp"
android:id="@+id/test_nameDR"
android:layout_marginTop="4dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Details Text"
android:textColor="@color/black"
android:textSize="14sp"/>
for Button
<package_name.fonts.Railway_Regular_Btn
android:id="@+id/revSubmit"
android:background="@color/greenDeep"
android:layout_marginTop="-54dp"
android:layout_width="match_parent"
android:layout_height="54dp"
android:textColor="@color/white"
android:textSize="16dp"
android:text="SUBMIT REVIEW"
/>
for EditText
<package_name.fonts.Railway_Regular_EdTx
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="15dp"
android:textColorHint="@color/lightgray"
android:textSize="14dp"
android:background="@drawable/edit_text"
android:id="@+id/email_id"
android:hint="Email Id"
android:inputType="textEmailAddress" />
Second Method to Change your font :
Typeface typeface,typeface2;
typeface = Typeface.createFromAsset(this.getAssets(),"fonts/Raleway-Regular.ttf");
typeface2 = Typeface.createFromAsset(this.getAssets(),"fonts/Raleway-SemiBold.ttf");
button1.setTypeface(typeface);
edit_text1.setTypeface(typeface2);
Answered By - Abhishek Kumar
Answer Checked By - Marie Seifert (JavaFixing Admin)