3

I want to set different font style my ‘button’ text and ‘edit text box’ text like times New Roman, Calibri,Cambria and Georgia. How can i set different font,example for i want to change my login button text to Calibri font. I don't know how can i set or import font from MS Office or Font files. Please suggest me, Thank you..

MY XML CODE HERE

 <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint=" User Name " /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint=" Password " android:fontFamily="Tekton Pro Ext"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:fontFamily="Tekton Pro Ext" android:text=" Login " /> </LinearLayout> 

Layout

enter image description here

3 Answers 3

4

You need to create fonts folder under assets folder in your project and put your TTF into it and in the main or in your activity you can set the fonts as

TextView myTextView=(TextView)findViewById(R.id.textBox); Typeface typeFace=Typeface.createFromAsset(getAssets(),"fonts/mytruetypefont.ttf"); myTextView.setTypeface(typeFace); 
Sign up to request clarification or add additional context in comments.

Comments

1

Maybe its better to take a look at this posts which exactly answered what you are looking for:

1- How to change fontFamily of TextView in Android

2- How to change the font on the TextView?

for short you must do something like this : put the font in your assets folder in /fonts directory and then use this line of code:

Button bt = (Button) findViewById(R.id.myButton); Typeface face = Typeface.createFromAsset(getAssets(), "fonts/epimodem.ttf"); bt.setTypeface(face); 

Comments

0

All the above answers are right. If you want to use a Button or TextView with a different font throughout the app, here's what you do.

public class FontsArePriceyTextView extends TextView { public MyTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); setCostlyFont(); } public MyTextView(Context context, AttributeSet attrs) { super(context, attrs); setCostlyFont(); } public MyTextView(Context context) { super(context); setCostlyFont(); } private void setCostlyFont() { if (!isInEditMode()) { Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "filenameofyourfont.ttf"); setTypeface(tf); } } } 

Then in your layouts, replace TextView with yourpackagename.FontsArePriceyTextView. Eg.

<com.company.projectname.widgets.FontsArePriceyTextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Your awesome stylised text" /> 

You should be good to go.

3 Comments

How can i use all text fields this constructor class there is possible to access all text view for my project?
Just replace the TextView tags in XML Layouts with your custom Text View class name. You can use it as a normal text view, eg. TextView tv = (TextView) findViewById(R.id.stylisedTextView). I guess you can now implement the same for Buttons or whatever you want to yourself.
any possible to add direct xml textview for new font

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.