Canvas drawText (font) import Times font? - android

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

Related

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

The new Custom Font Method in android using xml

As android released its O preview, a new feature has been added called fonts in xml. Its easy to implement but i have few doubts.
let me first add the best way to do it
1.)Right-click the res folder and go to New > Android resource directory. The New
Resource Directory window appears.
2.)In the Resource type list, select font, and then click OK.
3.)Add your font files in the font folder.The folder structure below generates R.font.dancing_script, R.font.la_la, and R.font.ba_ba.
4.)Double-click a font file to preview the file's fonts in the editor.
Next we must create a font family
1.)Right-click the font folder and go to New > Font resource file. The New Resource File window appears.
2.)Enter the file name, and then click OK. The new font resource XML opens in the editor.
3.)Enclose each font file, style, and weight attribute in the font tag element. The following XML illustrates adding font-related attributes in the font resource 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/hey_regular" />
<font
android:fontStyle="italic"
android:fontWeight="400"
android:font="#font/hey_bababa" />
</font-family>
Adding fonts to a TextView:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
**android:fontFamily="#font/ba_ba"**/>
As from the documentation all the steps are correct.my question is:
1.)Can this work in android lollipop or marshmallow
2.)Is any support library needed for implementing the features
3.)What type of font extension will this support like .ttf .otf
Please give me the best explanation.
1.)Can this work in android lollipop or marshmallow
Ans : It's work for lower Version also(Based on my research).
2.)Is any support library needed for implementing the features
Ans : No need support Lib only you need to update your sdk(Android O)
3.)What type of font extension will this support like .ttf .otf
Ans: Yes, Its support .ttf, .otf font Files.
For more info you can see below links :
Android Doc for Font Family Api , Android-O Preview Video

Arial font for text in 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);
}

How to get Hindi fonts in Android..?

I'm new to Android field . I have followed the procedure of getting Hindi fonts on emulator but still Hindi characters are appearing in square boxes. Please help me how to proceed. I m using android 2.2 ,juno version. Any help regarding above problem will be highly appreciated.
Set The Hindi Font on TextView in Android
TextView t = new TextView(this);
Typeface Hindi = Typeface.createFromAsset(getAssets(), "fonts/mangle.ttf");
t.setTypeface(Hindi);
t.setText("Naveen Tamrakar");
Android does not yet fully support Hindi (droid fonts):
Read this link.
Create a folder in assets as "fonts".
Place your .ttf file of font in that.
then try the code as :
Typeface fontHindi = Typeface.createFromAsset(getAssets(), "fonts/font.ttf");
tv.setTypeface(fontHindi);
It works for me. Hope it works for you too.
You can download hindi fonts from http://hindi-fonts.com and copy it in /system/font directory of your phone. The font will be installed.
First of all download hindi font, d the OTF/TTF file you get, save that in asset folder, eclipse has already the folder, if you are using android studio, then make a new folder in project/app/src/main.
Then in java file, suppose you have to apply your font on TextView t1, use the code..
Typeface mytypeface= Typeface.createFromAsset(getAssets(),"font-file_name.TTF");
t1.setTypeface(mytypeface);
This way you can use Hindi font in Android.
Step 1: Add All Hindi text in res --> values --> string.xml file
<resources>
<string name="app_name">गन्ने से समृद्धि</string>
<string name="ganne_ki_kheti">गन्ने की खेती</string>
<string name="sampark">संपर्क</string>
Step 2: In layout xml file add text tag as android:text="#string/your_text".
For Example see below.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="32dp"
android:text="#string/ganne_ki_kheti"
android:textSize="32sp"
android:textStyle="bold" />

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