Using custom fonts. What have I missed? - android

I am trying to use a custom font on a TextView in Xamarin C#
I have the file coopbl.ttf (don't judge me, this was just to see if it worked!)
I have placed it in the following location root/Assets/Font/coopbl.ttf and marked Build Action to AndroidAssist
My XML looks as follows:
<TheKey.Classes.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="font"
android:textSize="50sp"
android:id="#+id/customFont"
custom:customFont="Font/coopbl.ttf"/>
When I call Typeface.CreateFromAsset(Context.Assets, "Font/coopbl.ttf"); I get the following exception:
Java.Lang.RuntimeException: Font asset not found Font/coopbl.ttf at
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw
I have followed much of the pattern used by Cheesebaron here, including the TextView.cs class: https://github.com/Cheesebaron/Cheesebaron.FontSample.
What have I missed?
Update based on comments
I tried the following with no luck, still seeing the same error:
Renamed Font directory to font then to fonts and then even tried Fonts for completeness
Removed Font/ from the method call
In Visual Studio, the Assets folder is located in the root of the project when a new Xamarin project is created, it is at the same level as Resources.

Related

Font Awesome is not working properly - how to fix that?

I wanted to create a test app that only displays a few font awesome icons.
I downloaded the files from here and added them to my assets folder. Then I created a helper class (IconTextView) as below that include the method:
public static Typeface getTypeface(Context context, String font) {
return Typeface.createFromAsset(context.getAssets(), font);
}
then in my main activity:
Typeface icon = IconTextView.getTypeface(this,"fa_brands_400.ttf" );
IconTextView iconTextView = (IconTextView) findViewById(R.id.iconTextView);
iconTextView.setTypeface(icon);
on my xml I have:
android:id="#+id/iconTextView"
android:layout_width="195dp"
android:layout_height="17dp"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:textColor="#color/black"
android:text=""
I get the values from this cheatsheet
but instead of the actual icon, I only see the text displayed. I've tried different answers that are posted already, but the references are for older faw versions and don't apply to the new codes. I tried passing a string reference on my xml as well but it didn't work. I tried downloading the files for web and then the ones for desktop use, but it didn;t make any difference. Any idea as to what I'm doing wrong?
Turns out it was an android studio mistake. I was setting the icon reference from the cheatsheet surrounded by &#x...; as suggested in other sources. Android studio was extracting my string resource as <![CDATA[&#x...;]]> in the strings.xml file. When I manually changed it to the former, it worked just fine!

'Resource.Designer.cs' doesn't open with any 'designer'

I'm using Visual Studio 2017 with the latest update.
I created a new Xamarin Forms solution (.net standard) and when I click on Resource.Designer.cs or when I try to "open with" with many options - I don't get any designer.
Since the text in the file says
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
Something must be missing.
How do I get the designer working?
According to an answer in the comments this is not like the Visual Studio designer file. It should not be edited, but also does not have a designer mode associated with it.

Nativescript and FontAwesome

I am trying to use icon font FontAwesome in Nativescript application, which is possible according to this article https://www.nativescript.org/blog/mobile-app-best-practices---use-font-instead-of-image-to-show-an-icon
I did everything that is described in that article:
Added .ttf in app/fonts
Added class in app.css
.fa{
font-family: "FontAwesome";
}
Used it in XML like so
text="" class="fa"
But result is rather disappointing:
I also tried the "\uf230" syntax, but that renders as plain text.
What am I doing wrong?
Could be a few things. Does it work on iOS? As your CSS definition is iOS compatible, not Android as Android needs the actual filename (without extension) whereas iOS needs the font name. So I have this to be xplatform-ready (the file is 'fontawesome-webfont.ttf'):
.fa {
font-family: 'FontAwesome', fontawesome-webfont;
}
The \f005 syntax is OK (<Label class="fa" [text]="'\uf005'"></Label>), but I'm using the splendid nativescript-ngx-fonticon plugin (there's also a non-Angular one) to be able to do this instead:
<Label class="fa" [text]="'fa-shopping-basket' | fonticon"></Label>
To make it work, you must make sure that the "fonts" directory is inside the "app" folder and that the following files exist:
font-awesome.eot
font-awesome.ttf
I opted to adopt this font as the default of my application, so I do not have to worry about where I'm going to use it and how much to enter the right class, everything is getting very good and the result is perfect.
In CSS, you only have to define a selector according to your interest for the source to be used, so just use the directive:
page {
font-family: 'FontAwesome'
}
Then where you want an icon, just use an html entity that represents it as it searches the site: http://fontawesome.io/icons/
See images:
You can see this video where I was based to start. It corrects in the video the extension used to be attentive.

Where to find default system icons in Nativescript?

I have created an empty Nativescript application with tns create myApp'. However,app/App_Resources/Android/drawable-hdpi` and other directories does not contain any default system icons. Thus, I cannot use them in the application. How can I find or install default system icons?
However, the default system icons are called using something like this:
<ActionItem ios:systemIcon="12" android:systemIcon="ic_menu_search" tap="showSearch" ios:position="right" android:position="actionBar" />
You can find a list (unfortunately without images) at https://developer.android.com/reference/android/R.drawable.html
For using your custom icons in the application, you have to add them in each drawable-XXX folder and then in your XML src="res://my_icon"
You can find more informations in the official documentation of NS : https://docs.nativescript.org/ui/ui-images#load-images-from-resource

Debugging pop up window "Missing Manifest?"

Im getting this error message. I created a new project and hit debug to test it. I added nothing and i get this pop up message. what could be my issue? I can create a new blank project wtih out the Controllers and Models projects in them it debugs just fine. But i need to use this solution provided to me.
New to mono droid any help will be apreciated.
You issue could be that none of the projects have been set to be deployed to a device.
Right click your Solution > Properties > Configuration Properties > Tick The Deploy checkbox for your Application Project if not already.
Also make sure to mark one of your activities with MainLauncher = true, otherwise Android does not know which Activity to start first. This is done in the [Activity()] attribute like so:
[Activity(Label = "Epic Activity", MainLauncher = true, Icon = "#drawable/icon")]
public class Activity1 : Activity {}
what could be my issue?
You are using Visual Studio... Android plays much nicer with Eclipse.
Kidding aside, every Android application requires an AndroidManifest.xml file. It looks like you are using MonoDroid, and there are docs about creating the manifest file.
Also, make sure you have built the project.
AndroidManifest.xml is generated as part of the build process, and the XML found within Properties\AndroidManifest.xml is merged with XML generated based on custom attributes.

Categories

Resources