This question already has answers here:
Using custom font in android TextView using xml
(11 answers)
Closed 9 years ago.
hi there i am newer in android and i want to ask how to use one font for many text views i have use this method
final String DosisPath = "fonts/Dosis-Bold.ttf";
final Typeface Dosis_Bold = Typeface.createFromAsset(getAssets(), DosisPath);
TextView txtCancle = (TextView) findViewById(R.id.cancle);
txtCancle.setTypeface(Dosis_Bold);
and it's work good but i try to make the same Typeface for another TextView but that dosen't work like this
final String DosisPath = "fonts/Dosis-Bold.ttf";
final Typeface Dosis_Bold = Typeface.createFromAsset(getAssets(), DosisPath);
TextView txtCancle = (TextView) findViewById(R.id.cancle);
txtCancle.setTypeface(Dosis_Bold);
EditText ETCode = (EditText)findViewById(R.id.secET);
ETCode.setTypeface(Dosis_Bold);
if the is an error i have done tell me and if there is another way to make that tell me too thanks alot .
This is how you should do it.
Create a class
public class TextViewContent extends TextView {
public TextViewContent(Context context) {
super(context);
setCustomFont(context);
}
public TextViewContent(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context);
}
public TextViewContent(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
if (isInEditMode()) {
return;
}
setCustomFont(context);
}
private void setCustomFont(Context ctx) {
setTypeface(Typeface.createFromAsset(ctx.getAssets(),
"Champagne_Limousines.ttf"));
}
}
and in your layout file
<com.hardik.test.widget.TextViewContent
style="#style/TextMedium"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:layout_marginTop="5dip"
android:gravity="center"
android:text="#string/tag_line" />
This will display textview in chosen font. Same way for EditText you can extends the EditText class and rest is same.
Related
This question already has answers here:
Android - Using Custom Font
(21 answers)
Closed 3 years ago.
I would like to know if it is possible to set a fontFamily variable in Android Studio like you can do with Strings.
I don't want to do anything fancy like custom fonts. I just want to have the convenience of being able to change the fontFamily of every textView at once.
So far I was not able to find a font tag. I hope that some of you can help me.
Best regards and thanks in advance :)
Make a Custom TextView Class and use it in textview
public class MetaFont extends TextView {
public MetaFont(Context context, AttributeSet attributeSet, int i) {
super(context, attributeSet, i);
a(attributeSet);
}
public MetaFont(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
a(attributeSet);
}
public MetaFont(Context context) {
super(context);
a(null);
}
private void a(AttributeSet attributeSet) {
if (attributeSet != null) {
TypedArray obtainStyledAttributes = getContext().obtainStyledAttributes(attributeSet, ATTRS);
setTypeface(Typeface.createFromAsset(getContext().getAssets(), "Comfortaa.ttf"));
obtainStyledAttributes.recycle();
}
}
}
Put the font otf file to /assets/fonts, create a utils for dealing with fonts like:
public class TypefaceUtils {
private static Typeface myFont1;
private static Typeface myFont2;
public static Typeface getMyFont1() {
return myFont1;
}
public static Typeface getMyFont2() {
return myFont2;
}
public static void initTypeface(Context context) {
if (context != null) {
myFont1 = Typeface.createFromAsset(context.getAssets(), "fonts/myfont1.otf");
myFont2 = Typeface.createFromAsset(context.getAssets(), "fonts/myfont2.otf");
}
}
}
Init fonts when onCreate your MainAcitivity
TypefaceUtils.initTypeface(this);
Set the font where you need, like:
textView.setTypeface(TypefaceUtils.getMyFont1());
Is there a way to change how the detection of autoLink TextView finds phone numbers?
Thing is, it detects international format quite well, like +49123456789 but it fails on local formatted numbers like 0699777666555 (without a preceeding "+" character).
We need to have those numbers available too.
The TextView is set up with autoLink="all"
<TextView
android:id="#+id/chat_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
... some other settings ...
android:autoLink="all"
android:linksClickable="true"
android:textColorLink="#color/darkblue"
android:textColor="#color/black"/>
We have internal numbers (like 5532) and local phone numbers without any prefixes like 12345678. It would be great, if they can be highlighted too, without any, or at least without too much coding involved.
Any solutions to this?
Thanks in advance!
Try to do this programatically:
public class AutoLinkifyTextView extends TextView {
public AutoLinkifyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AutoLinkifyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public void setText(String text) {
super.setText(text);
parseLinks();
}
#Override
public void setText(int stringRes) {
super.setText(stringRes);
parseLinks();
}
private void parseLinks() {
Linkify.addLinks(this, Linkify.ALL);
}
}
and then use AutoLinkifyTextView instead of TextView
I want to change the font of my complete app!
But I don't know how to access the complete application.
I have a way to access a text by Id, how can I change the code to access the whole App?
public class ExternalFont extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String fontPath = "fonts/FreeUniversal-Regular.ttf";
TextView txtUniversal = (TextView) findViewById(R.id.universal);
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
txtUniversal.setTypeface(tf);
}
}
To change all typeface for all TextView in app you should create custom style then add it into AppTheme style.
`<style name="customTextVeiw" parent="Widget.MaterialComponents.TextView">
<item name"fontFamilt">#font/custom_free_universal_regular</item>`
then add this style inside style.xml file inside AppTheme style.
`<style name="AppTheme" parent="AppBaseTheme">
<item name="android:textViewStyle">#style/customTextVeiw</item>`
Unfortunately there is no direct way in which you can change the typeface of all the TextViews you are using in your app by just changing the default typeface.
What you can do is create your own custom TextView and set typeface on it as suggested by #josedlujan. But the flaw in that approach is that each time a TextView object will be created, a new Typeface object will be created which is extremely bad for RAM usage (A Typeface object typically varies from 8-13 kb for typefaces like Roboto). So it is better to cache your Typeface once it has got loaded.
Step 1: Create a Typeface cache for your app -
public enum Font {
// path to your font asset
Y("fonts/Roboto-Regular.ttf");
public final String name;
private Typeface typeface;
Font(String s) {
name = s;
}
public Typeface getTypeface(Context context) {
if (typeface != null) return typeface;
try {
return typeface = Typeface.createFromAsset(context.getAssets(), name);
} catch (Exception e) {
return null;
}
}
public static Font fromName(String fontName) {
return Font.valueOf(fontName.toUpperCase());
}
Step 2: Define your own custom attribute "customFont" in attrs.xml for your CustomTextView
attrs.xml -
<declare-styleable name="CustomFontTextView">
<attr name="customFont"/>
</declare-styleable>
<com.packagename.CustomFontTextView
android:id="#+id/some_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:customFont="rl"/>
Step 3: Create your own custom TextView
public class CustomFontTextView extends TextView {
public CustomFontTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs);
}
public CustomFontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public CustomFontTextView(Context context) {
super(context);
init(context, null);
}
private void init(Context context, AttributeSet attrs) {
if (attrs == null) {
return;
}
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomFontTextView, 0 ,0);
String fontName = a.getString(R.styleable.CustomFontTextView_customFont);
String textStyle = attrs.getAttributeValue(styleScheme, styleAttribute);
if (fontName != null) {
Typeface typeface = Font.fromName(fontName).getTypeface(context);
if (textStyle != null) {
int style = Integer.decode(textStyle);
setTypeface(typeface, style);
} else {
setTypeface(typeface);
}
}
a.recycle();
}
To use Font for all app text you should use the Fonts in XML feature on devices running Android 4.1 (API level 16) and higher.
To add fonts as resources, perform the following steps in the Android Studio:
Right-click the res folder and go to New > Android resource directory.
The New Resource Directory window appears.
In the Resource type list, select font, and then click OK.
Add your font files in the font folder like this free_universal_regular.ttf
you should start your font with lower and under score instead of -.
Creating a font family
Right-click the font folder and go to New > Font resource file. The New Resource File window appears.
Enter the file name e.g: custom_free_universal_regular.xml , and then click OK. The new font resource XML opens in the editor.
add this in custom_free_universal_regular.xml file.
<?xml version="1.0" encoding="utf-8"?> <font-family xmlns:android="http://schemas.android.com/apk/res/android"> <font android:fontStyle="normal" android:fontWeight="400" android:font="#font/free_universal_regular" /> </font-family>
Now we can use this font family anywhere in our app like this
For e.g for all app use it inside the style.xml
<style name="BaseTheme" parent=""> <item name="fontFamily">#font/custom_free_universal_regular</item>
or in a specific element use it like this
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:fontFamily="#font/custom_free_universal_regular"/>
I hope that could help.
Create your own Textview with custom style.
Example:
public class YourTextView extends TextView {
public YourTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public YourTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public YourTextView(Context context) {
super(context);
init();
}
private void init() {
Typeface tf = Typeface.createFromAsset(context.getAssets(),
"fonts/yourfont.ttf");
setTypeface(tf);
}
}
You can use in yours XML.
I need to add custom typeface for the listview items. And I have added the values to the list using using adapter like this
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), newList, R.layout.club_list2, from, to);
Here I want to set typeface for the textviews in club_list2 layout. How is it possible?
If you want set your custom typeface from xml you can do this
public class TypefaceTextView extends TextView {
private static Map<String, Typeface> 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>();
}
// prevent exception in Android Studio / ADT interface builder
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();
}
}
}
in xml.
<com.example.TypefaceTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="30sp"
android:text="#string/hello_world"
geekui:customTypeface="fonts/yourfont.ttf" />
and your font should be in asset/fonts/ folder
create resource file inside values and set it as name attrs.xml
then copy past this there
<resources>
<declare-styleable name="TypefaceTextView">
<attr name="customTypeface" format="string" />
</declare-styleable>
</resources>
If you want them all to use the same one, use android:typeface in the xml. If you want themto use different ones, set it on the TextView in your getView function.
the default is not Arial. The default is Droid Sans.
change to a different built-in font
Second, to change to a different built-in font, use android:typeface in layout XML or setTypeface() in Java(android).
Example
Typeface tf = Typeface.createFromAsset(getAssets(),
"fonts/Arial.otf");
TextView tv = (TextView) findViewById(R.id.CustomFontText);
tv.setTypeface(tf)
This question already has answers here:
Android: Want to set custom fonts for whole application not runtime
(12 answers)
Custom fonts and XML layouts (Android)
(18 answers)
Closed 9 years ago.
How can I use a custom font which was added in the asset folder in my xml? I know we can use setTypeface() method in java, but we have to do this everywhere where we use that TextView. So is there a better way?
The best way i found by googling is- Say if you want to use in TextView then we have to extend the Textview and have to set the font in that later we can use our customised Textview in our xml. I'll show the extended TextView below
package com.vins.test;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyTextView(Context context) {
super(context);
init();
}
private void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
"your_font.ttf");
setTypeface(tf);
}
}
We calling init() to set font in each of the costructors.
Later we have to use this in our main.xml as shown below.
<com.vins.test.MyTextView
android:id="#+id/txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="This is a text view with the font u had set in MyTextView class "
android:textSize="30dip"
android:textColor="#ff0000"
>
Update:
Be aware about the memory leak in pre-4.0 Android as mentioned by pandre.
Put your font file in asset\fonts\fontname
Define three textview in your xml file then, put this code in your activity class:
public class AndroidExternalFontsActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Font path
String fontPath = "fonts/DS-DIGIT.TTF";
String fontPath1 = "fonts/Face Your Fears.ttf";
String fontPath2 = "fonts/HelveticaNeue-Bold_0.otf";
// text view label
TextView txtGhost = (TextView) findViewById(R.id.ghost);
TextView txtGhost1 = (TextView) findViewById(R.id.ghost1);
TextView txtGhost2 = (TextView) findViewById(R.id.ghost2);
// Loading Font Face
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
Typeface tf1 = Typeface.createFromAsset(getAssets(), fontPath1);
Typeface tf2 = Typeface.createFromAsset(getAssets(), fontPath2);
// Applying font
txtGhost.setTypeface(tf);
txtGhost1.setTypeface(tf1);
txtGhost2.setTypeface(tf2);
}
}