Arial font for text in Android - android

I want to show text in Arial font. But Arial font is not available in android system fonts. I don't want to use arial ttf file in my application. Is there any other way to apply text with Arial font.

If the font is not available in the android system, then you have to use the font file to apply that particular font say arial to your textView. May I know why you are not willing to use the font file to apply as it gives the same functionality.
Sample usage for using the font file is:
Typeface tfArial = Typeface.createFromAsset(getAssets(), "arial.ttf");
TextView tv = null;
// find the textview id from layout or create dynamically
tv.setTypeface(tfArial);
EDIT:
You need to put the font file arial.ttf in your asset folder.

Download Arial font and in assets create folder name with "fonts" and save font at there
Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/arial.ttf");
TextView tv = (TextView) findViewById(R.id.CustomFontText);
tv.setTypeface(tf);

You can create your custom font just adding the .ttf file in "fonts" folder inside "res":
The xml files contain this:
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
<font
android:font="#font/nunito_extrabold"
android:fontStyle="normal"
android:fontWeight="400" />
</font-family>
And that's all! You can use this font in every TextView just adding android:fontFamily property.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/font_semibold"
android:text="#string/custom_font" />

You can add google font family from android studio by going to
xml design
add font family then there is limitem font family so click on
more font family ( this is google font you can choose either add font or downloadable font)
add font then click ok then the font will be added in your font list and you can use like default font.

Consider using Calligraphy to have any custom font in your Android application.
You will not need to add code in each TextView to apply a font. Simply initialise at application startup and you're good to go.

Create a TextView from Java Code or XML like this
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:textSize="15sp"
android:textColor="#color/tabs_default_color"
android:gravity="center"
android:layout_height="match_parent"
/>
Make sure to keep the id as it is here because the TabLayout check for this ID if you use custom textview
Then from code inflate this layout and set the custom Typeface on that textview and add this custom view to the tab
for (int i = 0; i < tabLayout.getTabCount(); i++) {
//noinspection ConstantConditions
TextView tv=(TextView)LayoutInflater.from(this).inflate(R.layout.custom_tab,null)
tv.setTypeface(Typeface);
tabLayout.getTabAt(i).setCustomView(tv);
}

Related

Canvas drawText (font) import Times font?

I'm trying to use Times as font for the drawText()-method of the Canvas element. However couldn't find a solution yet.
There is the possibility to set fonts like 'sans-serif' or 'casual' by using following code:
paint.setTypeface(Typeface.create("casual",Typeface.NORMAL));
However trying to use Times or Arial etc. doesn't work.
Do I have to import these fonts first by myself?
Would appreciate if You have a solution for this.
Thank You in advance!
First you need to create an 'assets' folder under the 'main' folder and put a font file like .ttf file in it.
assets folder path
this is casual font download link
https://befonts.com/casual-font.html
I hope this is the answer you want. thank you
example)
Typeface mTfRegular = Typeface.createFromAsset(getContext().getAssets(), "OpenSans-Regular.ttf");
setTypeface(mTfRegular)
Another easy way to do this is to use the font family.
this way,
Create a folder called font in the /app/src/main/res/ path, put the font file inside, and write the contents of the Font Family in xml in /app/src/main/res/xml, and then apply the TextView or EditText you want to apply. Apply it using the android: fontFamily property.
Writing the Font Family in XML,
<?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/casual_regular" />
<font
android:fontStyle="italic"
android:fontWeight="400"
android:font="#font/casual_italic" />
</font-family>
Apply Fonts to FontView Using TextView,
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/casual"/>
This helped me to solve it:
1 - Creating a font directory in resources (res) folder.
2 - Drag & Drop .tff file in the font directory (example.tff).
And do following:
Typeface myFont = ResourcesCompat.getFont(this, R.font.times);
Source (see accepted answer): Link

Adding costum font to android in font family xml

In the android guides for Fonts in XML, they give the example of font family XML you create by adding:
android:font="#font/lobster_regular"
Which I think its the font you added to your assets/font folder. But when i try it I don't get it why it isn't able to find the font I've added. It only fins sans, serif and mono-space. Anyone have a clue what it might be the problem?
I know there are other ways to add a custom font, but due to the material calendar that I want to use, I need it to define in the styles XML, only way to customize it. I tried calligraphy lib, but does not help.
Using typeface you easily set font family
Typeface type = Typeface.createFromAsset(getAssets(),
"fonts/fontname");
now set this typeface to your textview
tv.setTypeface(type);
Add this to your activity code:
TextView tv = (TextView) findViewById(R.id.appname);
Typeface face = Typeface.createFromAsset(getAssets(),
"fonts/fontname");
tv.setTypeface(face);
Note that for this you'll have to create a fonts folder at app/src/main/assets

Custom font not showing on device

I am trying to add a custom font following the "Fonts in XML" tutorial on this site. I followed the tutorial to the letter and checked many times to find something I missed but I just can't see it. The font I added is a TTF file.
In the designer, I can select my font family and the text in the TextView changes to the custom font I added. However, when I run the app on my device, the text is defaulted to the regular font. This doesn't happen with the fonts included in Android Studio.
Additional question: when I tried yet another custom font, the text in the TextView changed to the custom font, but the text itself (the content) also changed to some gibberish. Is this an indication of a bad font or something else?
sv_regular.xml
<?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/sv_font_regular" />
</font-family>
TextView
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fontFamily="#font/sv_regular"
android:text="#string/app_name" />
You can do this programmatically
Add external fonts in your assets folder
1. Go to the (project folder)
2. Then app>src>main
3. Create folder 'assets>fonts' into the main folder.
4. Put your 'abc.ttf' into the fonts folder.
TextView tx = (TextView)findViewById(R.id.textview1);
Typeface custom_font = Typeface.createFromAsset(getAssets(),"fonts/abc.ttf");
tx.setTypeface(custom_font);
I have encountered such a problem when the font is displayed in Android Studio but does not work on a physical device.
So if you want a custom font from Android Studio to be displayed on your phone screen, you need to add this font to your project as follows:
Open res folder.
Create Android Resource Directory. Resource type - font. You can also name your new folder font.
Then simply drag and drop your ttf font into the font folder. Important: The font name must not contain prohibited characters, and all letters in the name must be lowercase. The name of my font in this example: kopenhagen
(you may have a different font name).
Open your xml file, find the text in which you want to change the font, and just add the following:
android:fontFamily="#font/kopenhagen"
*Instead of the word kopenhagen, use the name of your font
Does your font exactly support the language of the text? This problem occurs when the font does not support certain characters, such as Cyrillic. And one more: try use sv_font_regular in TextView
android:fontFamily="#font/sv_font_regular"
Example

How to display telugu font in android

I want to display Telugu font in android In 4.0 and above devices Telugu font will support automatically but I have problem in 4.0 below devices.I have used following code but Telugu is not rendering properly.
Typeface tf= Typeface.createFromAsset(this.getAssets(), "fonts/gautami.ttf");
TextView telugu=(TextView)findViewById(R.id.telugu);
telugu.setTypeface(tf);
telugu.setText("హైదరాబాద్");
Please help me on this. Thanks in advance.
remove /font to solve your problem
You can use a subfolder called fonts but it must go in the assets folder not the res folder.
assets/res
Android
TextView telugu = (TextView) findViewById(R.id.telugu);
Typeface Typefacetf = Typeface.createFromAsset(getAssets(), "gautami.ttf");
telugu.setTypeface(Typefacetf);
telugu.setText("హైదరాబాద్");
Just define your font in string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="text_your_name">హైదరాబాద్</string>
</resources>
because in that encoding is utf-8 is there so it will render properly.
now for setting that text you can use this:
telugu.setText(getResources().getString(R.string.text_your_name));
EDIT:
Now see answer over here it may help you out :: stackoverflow they having same problem like you.
Write a item inside string.xml inside values folder
<string name="your_text">హైదరాబాద్</string>
Then keep your font file in assets folder
Then create custom typeface in java code using font from assets folder
Typeface custom_type_face = Typeface.createFromAsset(getAssets(), "gautami.ttf");
Use this to your TextView
TextView telugu = (TextView) findViewById(R.id.telugu);
telugu.setTypeface(custom_type_face);
Set Text from strings.xml folder
telugu.setText(getResources().getString(R.string.your_text));

Html in text view with different fonts for bold and italic

I'm trying to use a custom font on a TextView. The TextView text is set with textView1.setText(Html.fromHtml(htmlText));
The html contains bold and italic spans
Now. I purchased a custom font. The font comes with 3 different files (ttf). One for regular, one bold and for italic.
How can I apply those three font files to the textview?
This link will help you to see how to customize android font: http://mobile.tutsplus.com/tutorials/android/customize-android-fonts/
In which concerns how to apply those font files to the textview, you need to integrate them first in your project:
Typeface tf = Typeface.createFromAsset(this.getAssets(),
"fonts/xxx.TTF");
txt1.setTypeface(tf);
The ttf file should be placed in --> assets/fonts/xxx.TTF
All needed details are in the paragraph: "Using Custom Fonts"
The best way right now for API 16+ is to define a font resource file if you are using Support Library above v26 or the new AndroidX libraries, basically you add your normal and italic ttf font files in the fonts folder and create a font resource xml and basically make it look something like
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res-auto">
<font
app:fontStyle="normal"
app:fontWeight="400"
app:font="#font/custom_regular_font" />
<font
app:fontStyle="italic"
app:fontWeight="400"
app:font="#font/custom_italic_font" />
<font
app:fontStyle="normal"
app:fontWeight="700"
app:font="#font/custom_bold_font" />
</font-family>
the last one is for bold fonts, apply this xml suppose custom_font_family.xml to your textview as android:fontFamily="#font/custom_font_family", now any html text you set with fromHtml with any of the three span types will use the proper fonts, when needed, this allows you to even have a custom font family mixing entirely different fonts and doesn't quite literally have to be from the same family.
I imagine you want to do a quick refactor on your code in order to incorporate the assets.
I would extend TextView and attempt to parse the HTML and apply the proper typeface at onDraw.
Override setText and parse the parameter creating a Map for character and the proper typeface that should be used.
Then, override onDraw and before drawing, change the typeface of super.getPaint() in accord to the Map you created on the previous step.
The code should look something as the one presented in onDraw method from How to correctly draw text in an extended class for TextView?, however you will set the previously determined typeface instead of applying super.getTypeface().
Hope it helps you
Have you try with applying that all font to same textView's text one by one.
I think with that you can apply the more more effect to same TextView.
Milos's code is right.
In addition i have puted my own explaination.
You can add your fonts in to the assets foldera and after that you can apply that font to the textView one-by-one.
Not sure but might be useful to you.
My Code:
Typeface font1 = Typeface.createFromAsset(getAssets(), "YOUR_FONT1.ttf");
Typeface font2 = Typeface.createFromAsset(getAssets(), "YOUR_FONT2.ttf");
Typeface font3 = Typeface.createFromAsset(getAssets(), "YOUR_FONT3.ttf");
chips_text.setTypeface(font1);
chips_text.setTypeface(font2);
chips_text.setTypeface(font3);
Feel free to comment and queries.

Categories

Resources