I want to use Bungee inline font style in my android xml code
<RelativeLayout
android:id="#+id/sound_setting_layout"
android:layout_width="500dip"
android:layout_height="350dip"
android:layout_marginTop="65dip"
android:layout_marginLeft="780dip"
android:layout_alignParentTop="true"
android:padding="10dip"
android:gravity="center"
android:visibility="gone"
android:background="#drawable/volume_layout"
>
<TextView
android:layout_width="450dip"
android:layout_height="50dip"
android:gravity="center_horizontal"
android:layout_alignParentTop="true"
android:text="Volume Control"
android:textStyle="bold"
android:textColor="#ffffff"
android:textSize="30dip"
/>
I tried a lot but i could not find font style Bungee in android.
We don't have default bungee font style in android so if you wanna use it download bungee font .ttf file and create a folder in assets named fonts and paste your downloaded font (.ttf) there
Here you can download Bungee font:https://djr.com/bungee/
In your code just do this
// Font path insted of bungee.ttf replace your .ttf file
String fontPath = "fonts/bungee.ttf";
// text view label which you want to apply Bungee font
TextView txtGhost = (TextView) findViewById(R.id.androidSample);
// here loading Font Face
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
// Applying font
txtGhost.setTypeface(tf);
load your font file to assets folder
then in your activities onCreate , use the following methods
Typeface face = Typeface.createFromAsset(YOUR_ACTIVITY.this.getAssets(),"fonts/YOUR_FONT_FILE_NAME.otf");
your_text_view.setTypeface(face);
If you're going to use custom fonts throughout your whole application, on multiple TextViews for example, it's bettter to use a Singleton pattern because re-instantiating the fonts over and over will slow down your application.
Try this class and replace the font path with your own custom fonts, make sure you have your custom fonts inside "assets" folder inside "main"
public class ProximaTypeface {
public static ProximaTypeface instance = new ProximaTypeface();
public ProximaTypeface() {
}
public Typeface regularTypeFace = null;
public Typeface semiBoldTypeFace = null;
public static ProximaTypeface getInstance() {
return instance;
}
public void getRegularTypeface(Context context, TextView textView) {
if (regularTypeFace == null) {
regularTypeFace = Typeface.createFromAsset(context.getResources().getAssets(), "fonts/proxima_nova_regular.otf");
}
textView.setTypeface(regularTypeFace);
}
public void getSemiBoldTypeface(Context context, TextView textView) {
if (semiBoldTypeFace == null) {
semiBoldTypeFace = Typeface.createFromAsset(context.getResources().getAssets(), "fonts/proxima_nova.otf");
}
textView.setTypeface(semiBoldTypeFace);
}
}
Then in your Activity:
ProximaTypeface proximaTypeface = new ProximaTypeface();
TextView myTextView = (TextView) findViewById(R.id.textView);
proximaTypeface.getRegularTypeface(context,myTextView);
Related
Error
error: style attribute 'app:attr/fontFamily' not found.
Message{kind=ERROR, text=error: style attribute 'app:attr/fontFamily' not found.
<style name="RadioButtonCustomStyle" parent="Widget.AppCompat.CompoundButton.RadioButton">
<item name="android:textColorPrimaryDisableOnly">#f44336</item>
<item name="android:textColor">#607ba3</item>
<item name="app:fontFamily">#font/raleway_medium</item>
</style>
I have added raleway_medium.ttf in the app/assets/font/raleway_medium.ttf
You can do it by puting your font at the assets folder and using the code below:
Typeface tf = Typeface.createFromAsset(getAssets(), "your_font.ttf");
yourTextView.setTypeface(tf);
There's a library called Calligraphy. It's used in cases like yours - to replace fonts in all views on older phones. If I'm correct, it doesn't support font resources, but just plain .ttf files. See: https://github.com/chrisjenx/Calligraphy
I'm working on a library with support for font resources for older phones. It's working cleaner than Calligraphy, but the library itself is very large, so it may not be best suited for you. The font support commit is well extracted and you can find it here: https://github.com/ZieIony/Carbon/commit/baefcfb1941ecc1b4e293f31f5220ab7abaf4584
And the essential part of the answer is the following method. I guess it was taken from Material Components sources. You can add it to your text fields and buttons to use it to handle the xml attribute.
private void handleFontAttribute(TypedArray appearance, int textStyle, int attributeId) {
WeakReference<android.widget.TextView> textViewWeak = new WeakReference<>(this);
AtomicBoolean asyncFontPending = new AtomicBoolean();
ResourcesCompat.FontCallback replyCallback = new ResourcesCompat.FontCallback() {
#Override
public void onFontRetrieved(#NonNull Typeface typeface) {
if (asyncFontPending.get()) {
android.widget.TextView textView = textViewWeak.get();
if (textView != null)
textView.setTypeface(typeface, textStyle);
}
}
#Override
public void onFontRetrievalFailed(int reason) {
}
};
try {
int resourceId = appearance.getResourceId(attributeId, 0);
TypedValue mTypedValue = new TypedValue();
Typeface typeface = ResourcesCompat.getFont(getContext(), resourceId, mTypedValue, textStyle, replyCallback);
if (typeface != null) {
asyncFontPending.set(true);
setTypeface(typeface, textStyle);
}
} catch (UnsupportedOperationException | Resources.NotFoundException ignored) {
}
}
Add your font in your folder font (app/res/font).
After that you can use a textview and set the font
<TextView
android:id="#+id/ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/YourFont"
android:text="#string/YourText"
android:textColor="#color/YourColor"
android:textSize="20dp" />
In your style you can try to change "app:fontFamily" with "android:fontFamily"
Hope this will help
I am trying to add a icon to my textview. My java code for the corresponding fragment is:
public class BlankFragment extends Fragment {
public BlankFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
"fonts/weathericons-regular-webfont.ttf");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
....
TextView name_sunrise = rootView.findViewById(R.id.tv_sunr);
//name_sunrise.setText(); Here I want to put an icon from tf
My xml for tv_sunr is defined as:
<TextView
android:id="#+id/tv_sunr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tv_sunrt"
android:layout_alignBottom="#+id/tv_sunrt"
android:layout_toEndOf="#+id/iv_image"
android:paddingLeft="10sp"
android:text="#string/sunrise"
android:textSize="20sp"></TextView>
And the text I am trying to replace is android:text="#string/sunrise"
defined in strings as:
<string name="sunrise">SunRise</string>
The question is twofold:
How to add the ttf to the textview
How to find the actual item in the ttf file I am looking to use (e.g. in this case, how to find the font for sunrise)
I am using this ttf from github.
I am following this tutorial, but stuck here.
UPDATE
Hi, from the weather icon, I have found that the icon I want to use is named as wi-sunset. The problem is how I will use that icon. Kindly help.
inside your onCreateView , you can set the font like this
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
"fonts/weathericons-regular-webfont.ttf");
TextView name_sunrise = rootView.findViewById(R.id.tv_sunr);
name_sunrise.setTypeface(tf);
create font folder inside your res folder and put your string files on font folder.
You can define font for your views like below.
XML :
android:fontFamily="#font/your_font" //no need to mention your file type (ttf,otf)
Java :
Typeface t =ResourcesCompat.getFont(activity, R.font.your_font);
Thanks
If you are using Android Studio 3.0, then You need to create the directory with font. in font directory you can put your .ttf file
After that use the below code:
<TextView
android:id="#+id/tv_shop_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:fontFamily="#font/montserrat_semibold"
android:text="Hair Cut + Hair Spa"/>
This question already has answers here:
How to set default font family for entire Android app
(16 answers)
Closed 9 years ago.
I have one application in Which I want to change font of text in whole application.
Is there anyway to change font of application Programmatically or with xml in manifest.?
Try this
1.place your ttf file in assets folder and add these lines to your java file
Typeface font = Typeface.createFromAsset(activity.getAssets(),"fonts/androidnation.ttf");
tv.setTypeface(font);
2.To set it through xml
XML Typeface
Place you fonts in fonts folder and then use the following code.
TextView tv = (TextView) findViewById(R.id.appname);
Typeface face = Typeface.createFromAsset(getAssets(),"fonts/epimodem.ttf");
tv.setTypeface(face);
This is the way to set font programmatically.
Create fonts folder in asset and paste the fonts whatever you want to paste.
Create one class. Name it Typesafe.java
public enum TypeSafe {
HELVETICANEUELTCOMBD,
HELVETICANEUELTCOMBDCN,
HELVETICANEUELTCOMCN,
}
After that create one method in your Activity or if you have the Utility class.
public void setTypeface(TextView textView, TypeSafe type, AssetManager assetManager){
if (TypeSafe.HELVETICANEUELTCOMBD.equals(type)) {
final Typeface typeface = Typeface.createFromAsset(assetManager, "fonts/HelveticaNeueLTCom-Bd.ttf");
textView.setTypeface(typeface);
} else if (TypeSafe.HELVETICANEUELTCOMBDCN.equals(type)) {
final Typeface typeface1 = Typeface.createFromAsset(assetManager, "fonts/HelveticaNeueLTCom-BdCn.ttf");
textView.setTypeface(typeface1);
}
}
Call these method in your activity.
setTypeface(yourtextView, TypeSafe.HELVETICANEUELTCOMLT, getAssets());
The easiest way to do it is creating your own TextView:
public class MyTextView extends TextView {
public DMTextView(Context context, AttributeSet attrs) {
super(context, attrs);
// you need your TypefaceFile "myTypeface.ttf" in the assets folder of your project
setTypeface(Typeface.createFromAsset(context.getAssets(),"myTypeface.ttf"));
}
// add the other constructors if you want
}
now, you can use it anywhere in your xml:
<com.your.package.MyTextView ... > just like normal textviews
It might be improved by caching the Typeface, so you don't have to create it again with every Reference to your TextView.
How do you change text/font settings in an Android TextView?
For example, how do you make the text bold?
To do this in the layout.xml file:
android:textStyle
Examples:
android:textStyle="bold|italic"
Programmatically the method is:
setTypeface(Typeface tf)
Sets the typeface and style in which the text should be displayed. Note that not all Typeface families actually have bold and italic variants, so you may need to use setTypeface(Typeface, int) to get the appearance that you actually want.
Here is the solution
TextView questionValue = (TextView) findViewById(R.layout.TextView01);
questionValue.setTypeface(null, Typeface.BOLD);
Simply you can do the following:
Set the attribute in XML
android:textStyle="bold"
Programatically the method is:
TextView Tv = (TextView) findViewById(R.id.TextView);
Typeface boldTypeface = Typeface.defaultFromStyle(Typeface.BOLD);
Tv.setTypeface(boldTypeface);
Hope this helps you thank you.
In XML
android:textStyle="bold" //only bold
android:textStyle="italic" //only italic
android:textStyle="bold|italic" //bold & italic
You can only use specific fonts sans, serif & monospace via xml, Java code can use custom fonts
android:typeface="monospace" // or sans or serif
Programmatically (Java code)
TextView textView = (TextView) findViewById(R.id.TextView1);
textView.setTypeface(Typeface.SANS_SERIF); //only font style
textView.setTypeface(null,Typeface.BOLD); //only text style(only bold)
textView.setTypeface(null,Typeface.BOLD_ITALIC); //only text style(bold & italic)
textView.setTypeface(Typeface.SANS_SERIF,Typeface.BOLD);
//font style & text style(only bold)
textView.setTypeface(Typeface.SANS_SERIF,Typeface.BOLD_ITALIC);
//font style & text style(bold & italic)
From the XML you can set the textStyle to bold as below
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bold text"
android:textStyle="bold"/>
You can set the TextView to bold programmatically as below
textview.setTypeface(Typeface.DEFAULT_BOLD);
For case where you are using custom fonts, but do not have bold typeface for the font you can use:
myTextView.setText(Html.fromHtml("<b>" + myText + "</b>");
Set the attribute
android:textStyle="bold"
It's very easy
setTypeface(Typeface.DEFAULT_BOLD);
If you're drawing it then this will do it:
TextPaint.setFlags(Paint.FAKE_BOLD_TEXT_FLAG);
In the ideal world you would set the text style attribute in you layout XML definition like that:
<TextView
android:id="#+id/TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"/>
There is a simple way to achieve the same result dynamically in your code by using setTypeface method. You need to pass and object of Typeface class, which will describe the font style for that TextView. So to achieve the same result as with the XML definition above you can do the following:
TextView Tv = (TextView) findViewById(R.id.TextView);
Typeface boldTypeface = Typeface.defaultFromStyle(Typeface.BOLD);
Tv.setTypeface(boldTypeface);
The first line will create the object form predefined style (in this case Typeface.BOLD, but there are many more predefined). Once we have an instance of typeface we can set it on the TextView. And that's it our content will be displayed on the style we defined.
I hope it helps you a lot.For better info you can visit
http://developer.android.com/reference/android/graphics/Typeface.html
Through XML:
android:textStyle="bold"
Through Java:
//Let's say you have a textview
textview.setTypeface(null, Typeface.BOLD);
Define a new style with the format you want in the style.xml file in the values folder
<style name="TextViewStyle" parent="AppBaseTheme">
<item name="android:textStyle">bold</item>
<item name="android:typeface">monospace</item>
<item name="android:textSize">16sp</item>
<item name="android:textColor">#5EADED</item>
</style>
Then apply this style to the TextView by writing the following code with the properties of the TextView
style="#style/TextViewStyle"
The best way to go is:
TextView tv = findViewById(R.id.textView);
tv.setTypeface(Typeface.DEFAULT_BOLD);
in file .xml, set
android:textStyle="bold"
will set text type is bold.
4 ways to make Android TextView bold- Full answer is here.
Using android:textStyle attribute
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEXTVIEW 1"
android:textStyle="bold"
/>
Use bold|italic for bold and italic.
using setTypeface() method
textview2.setTypeface(null, Typeface.BOLD);
textview2.setText("TEXTVIEW 2");
HtmlCompat.fromHtml() method, Html.fromHtml() was deprecated in API level 24.
String html="This is <b>TEXTVIEW 3</b>";
textview3.setText(HtmlCompat.fromHtml(html,Typeface.BOLD));
Assuming you are a new starter on Android Studio,
Simply you can get it done in design view XML by using
android:textStyle="bold" //to make text bold
android:textStyle="italic" //to make text italic
android:textStyle="bold|italic" //to make text bold & italic
You can use this for font
create a Class Name TypefaceTextView and extend the TextView
private static Map mTypefaces;
public TypefaceTextView(final Context context) {
this(context, null);
}
public TypefaceTextView(final Context context, final AttributeSet attrs) {
this(context, attrs, 0);
}
public TypefaceTextView(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
if (mTypefaces == null) {
mTypefaces = new HashMap<String, Typeface>();
}
if (this.isInEditMode()) {
return;
}
final TypedArray array = context.obtainStyledAttributes(attrs, styleable.TypefaceTextView);
if (array != null) {
final String typefaceAssetPath = array.getString(
R.styleable.TypefaceTextView_customTypeface);
if (typefaceAssetPath != null) {
Typeface typeface = null;
if (mTypefaces.containsKey(typefaceAssetPath)) {
typeface = mTypefaces.get(typefaceAssetPath);
} else {
AssetManager assets = context.getAssets();
typeface = Typeface.createFromAsset(assets, typefaceAssetPath);
mTypefaces.put(typefaceAssetPath, typeface);
}
setTypeface(typeface);
}
array.recycle();
}
}
paste the font in the fonts folder created in the asset folder
<packagename.TypefaceTextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.5"
android:gravity="center"
android:text="TRENDING TURFS"
android:textColor="#000"
android:textSize="20sp"
app:customTypeface="fonts/pompiere.ttf" />**here pompiere.ttf is the font name**
Place the lines in the parent layout in the xml
xmlns:app="http://schemas.android.com/apk/res/com.mediasters.wheresmyturf"
xmlns:custom="http://schemas.android.com/apk/res-auto"
In my case, Passing value through string.xml worked out with html Tag..
<string name="your_string_tag"> <b> your_text </b></string>
editText.setTypeface(Typeface.createFromAsset(getAssets(), ttfFilePath));
etitText.setTypeface(et.getTypeface(), Typeface.BOLD);
will set both typface as well as style to Bold.
In Kotlin we can do in one line
TEXT_VIEW_ID.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
You can do this
ty.setTypeface(Typeface.createFromAsset(ctx.getAssets(), "fonts/magistral.ttf"), Typeface.BOLD);
textView.setPaintFlags(textView.getPaintFlags() | Paint.FAKE_BOLD_TEXT_FLAG)
To remove, use
textView.setPaintFlags(textView.getPaintFlags() & ~Paint.FAKE_BOLD_TEXT_FLAG)
Or in Kotlin:
fun TextView.makeBold() {
this.paintFlags = this.paintFlags or Paint.FAKE_BOLD_TEXT_FLAG
}
fun TextView.removeBold() {
this.paintFlags = this.paintFlags and (Paint.FAKE_BOLD_TEXT_FLAG.inv())
}
I have already read some articles and searched on Google, but I failed to do it.
My problem is regarding the font-face.
In Android, there are only 4 attributes in "android:typeface": Normal, Sans, Serif, Monospace.
So what do I have to do to use "Verdana" in my application?
Please suggest me a correct way to use this font in my Android application.
This is a simple example... create a folder in the root of your project called assets/fonts/ then paste the TTF font file (in this case Verdana.ttf). Then, if you want to apply that font to, say a TextView, do the following:
import android.graphics.Typeface;
public class FontSampler extends Activity {
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
TextView tv=(TextView)findViewById(R.id.custom);
Typeface face=Typeface.createFromAsset(getAssets(),
"fonts/Verdana.ttf");
tv.setTypeface(face);
}
}
This example was taken from the ComonsWare book (written by Mark Murphy). You can download the full example from GitHub.
You can use PixlUI at https://github.com/neopixl/PixlUI
import their .jar and use it in XML
<com.neopixl.pixlui.components.textview.TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world"
pixlui:typeface="GearedSlab.ttf" />
Well!!
This question is pretty old but still if someone is looking for the answer(in 2015) on how to apply custom font to all the Textviews through xml code directly see below:
First:
we need to add custom font inside assets folder inside your app directory:
.ttf or .otf both work in case of Android
Second:
Create Class CustomTextView which extends TextView like below:
public class CustomTextView extends TextView {
public CustomTextView(Context context) {
super(context);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public void setTypeface(Typeface tf) {
super.setTypeface(FontCache.getFont(getContext(),"fonts/<font_name>"));
}
}
Third:
FontCache class being used inside CustomTextView's setTypeface() method.Purpose is to do basic Font Caching using HashMap:
public class FontCache {
private static Map<String,Typeface> fontMap = new HashMap<String,Typeface>();
public static Typeface getFont(Context context,String fontname){
if(fontMap.containsKey(fontname)){
return fontMap.get(fontname);
}
else{
Typeface tf = Typeface.createFromAsset(context.getAssets(),fontname);
fontMap.put(fontname,tf);
return tf;
}
}
}
Fourth:[Final step]
All we do now is use the CustomTextView directly inside our xml file wherever custom font textview is required:
<<package_name>.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Custom Font Text"
android:textSize ="18sp"
android:textAppearance="?android:textAppearanceSmall"
android:id="#+id/custom_txt"
/>
Sorry, if this has already been posted somewhere on SO. Just thought to share if it helps someone!!
You can use simple EasyFonts third party library to set variety of custom font to your TextView. By using this library you should not have to worry about downloading and adding fonts into the assets/fonts folder. Also about Typeface object creation.
This library does not provides Verdana Font face.
But provide following font faces. Which might you would like to use.
Roboto
Droid Serif
Droid Robot
Freedom
Fun Raiser
Android Nation
Green Avocado
Recognition
Simply:
TextView myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setTypeface(EasyFonts.robotoThin(this));
I am author of this library.
To change the (custom) font of your app globally, have a look at Calligraphy
Simply add Calligraphy to your gradle.build and add the following snippet to your Application.onCreate():
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/MyCustomFont.ttf")
.setFontAttrId(R.attr.fontPath)
.build()
);
and in every Activity add the following:
#Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
That is all you need to do to change the font globally in your App. Have a look at the docs for more details.
// My example show you how to change fonts into a normal textView or list view
create a fonts folder into your assets dir of android and copy your custom font in that ..
assets/fonts/monaco.ttf
// Font path
String fontPath = "fonts/monaco.ttf";
// Loading Font Face
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
// CASE 1 : Inside your list view
holder.name = (TextView) convertView
.findViewById(R.id.textView_cityName);
// set name of text in each row
holder.name.setText(CitiesNames.get(position));
// set the type of font you want to set
holder.name.setTypeface(tf);
// CASE 2 : Inside your text view
TextView tx = (TextView)findViewById(R.id.textview1);
tx.setTypeface(tf);
//vKj
TextView textView = (Textview) findViewById(R.id.mytext);
Typeface face=Typeface.createFromAsset(getAssets(),
"fonts/Verdana.ttf");
textView.setTypeFace(face);